• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Hibernate exception for one-to-one mapping

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm using Hibernate 3.1.1 and MySQL 5.0.

Following is my first hibernate program for one-to-one mapping as follows -

Employee.java
package emp;

public class Employee {
private Long empId;

private String ename;
private String city;
private int salary;

public Employee() {
}
/****************************************************/
public Long getEmpId() {
return empId;
}

public void setEmpId(Long empId) {
this.empId = empId;
}
/****************************************************/
public String getEname() {
return ename;
}

public void setEname(String ename) {
this.ename = ename;
}

/****************************************************/

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

/****************************************************/
public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}
/****************************************************/
Machine machine=new Machine();

public Machine getMachine() {
return machine;
}

public void setMachine(Machine machine) {
this.machine = machine;
}

}

Machine.java

package emp;

import java.util.*;
public class Machine {

private String macId;
private String ipAddr;
//private String empId;

public Machine () {
System.out.println("in cons of mac");
}

Set empId= new HashSet();
/****************************************************/
public String getMacId() {
return macId;
}

public void setMacId(String macId) {
this.macId = macId;
}

/****************************************************/
public String getIpAddr() {
return ipAddr;
}

public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}

}

EmployeeManager.java

package emp;
import org.hibernate.*;
import java.util.*;

import util.HibernateUtil;

public class EmployeeManager {
static Employee empObj=null;
static Machine mac = null;
/******************************************************/
public static void main(String[] args) {
EmployeeManager mgr = new EmployeeManager();

if (args[0].equals("storeMac")) {
mgr.createAndStoreMac(args[1],args[2]);
System.out.println("mac data stored...");
}
else if (args[0].equals("storeEmp")) {
String macid=mgr.createAndStoreMac(args[1],args[2]);
mgr.createAndStoreEmp(args[3],args[4],Integer.parseInt(args[5]),macid);
}
/******************************************************/
private Long createAndStoreEmp(String name, String city, int salary, String macId)
{
System.out.println("in emp...");
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

empObj= new Employee();

empObj.setEname(name);
empObj.setCity(city);
empObj.setSalary(salary);

Machine macObj = (Machine) session.load(Machine.class, macId);

empObj.setMachine(macObj);
session.save(empObj);
System.out.println("session id in create..........."+session);

session.getTransaction().commit();
System.out.println("in emp tx commited...");
return empObj.getEmpId();
}

/*************************************************************/
private String createAndStoreMac(String macId, String ipAddr)
{
System.out.println("in store mac...");
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

Machine mac= new Machine();

mac.setMacId(macId);
mac.setIpAddr(ipAddr);
session.save(mac);

session.getTransaction().commit();
System.out.println("in dept... tx done");
return mac.getMacId();
}
}

Employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">;

<hibernate-mapping>

<class name="emp.Employee" table="myEmployee">

<id name="empId" column="EMP_ID">
<generator class="native"/>
</id>

<property name="name" column="NAME" type="string"/>
<property name="city" column="EMP_City"/>
<property name="salary"/>
<one-to-one name="macId" column= "MAC_ID" class= "emp.Machine" />

</class>

</hibernate-mapping>

Machine.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">;

<hibernate-mapping>

<class name="emp.Machine" table="myMachine">

<id name="macId" column="MAC_ID"/>

<property name="ipAddr" column="IP_ADDR" type="string"/>

</class>

</hibernate-mapping>

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">;

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.url">jdbc:mysql://localhost/Hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>

<mapping resource="emp/Employee.hbm.xml"/>
<mapping resource="emp/Machine.hbm.xml"/>

</session-factory>

</hibernate-configuration>


When I tried to run the above code I'm getting following exception -

in store mac...
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not read mappings from resource: emp/Employee.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at util.HibernateUtil.<clinit>(HibernateUtil.java:17)
at emp.EmployeeManager.createAndStoreMac(EmployeeManager.java:110)
at emp.EmployeeManager.main(EmployeeManager.java:19)
Caused by: org.hibernate.MappingException: Could not read mappings from resource: emp/Employee.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:484)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1453)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1421)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1402)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1378)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1298)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1284)
at util.HibernateUtil.<clinit>(HibernateUtil.java:13)
... 2 more
Caused by: org.hibernate.MappingException: invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:424)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:481)
... 9 more
Caused by: org.xml.sax.SAXParseException: Attribute "column" must be declared for element type "one-to-one".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:421)
... 10 more


Please let me know what might be the reason behind it.

Thanks for your time.

Nilesh
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While you don't hvae to repost.

There is a CODE button in the 10 buttons below Add Post/Reply. Please use this button to post your code, so that all indentation is kept. When you post code/xml without indentation, we just can't read it.

Anyway, luckily we don't have to read it.

"Caused by: org.hibernate.MappingException: invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:424)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:481)
... 9 more
Caused by: org.xml.sax.SAXParseException: Attribute "column" must be declared for element type "one-to-one"."

so for your one-to-one mapping in the Employee.hbm.xml is missing the "column" portion of that mapping.

Mark
 
Nilesh Yawale
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

Thanks for the reply. But if you see my Employee.hbm.xml file there is a property metioned as -

<one-to-one name="macId" column= "MAC_ID" class= "emp.Machine" />

Please let me know your comments on the same.

Thanks for your time.

Nilesh
 
Nilesh Yawale
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any Update....please.

Thanks for your time.

Nilesh Yawale
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nilesh Yawale:
Hi Mark,

Thanks for the reply. But if you see my Employee.hbm.xml file there is a property metioned as -

<one-to-one name="macId" column= "MAC_ID" class= "emp.Machine" />

Please let me know your comments on the same.

Thanks for your time.

Nilesh



OK, so maybe you do need to repost your code. Because if it isn't indented then I can't read it, that is why I didn't even look, to difficult to find stuff.

Thanks

Mark
 
Nilesh Yawale
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Employee.java



Machine.java



EmployeeManager.java



Employee.hbm.xml



Machine.hbm.xml



hibernate.cfg.xml

 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so it looks like you are having a one-to-one unidirectional association. In the Hibernate docs, it has you use a many-to-one with a "unique" attribute

http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html#assoc-unidirectional-121

for bi-directional one to one, here are the docs, but it also has the same many-to-one usage.

http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html#assoc-bidirectional-121

Basically, just using a <one-to-one> tag on just one side isn't the correct way to map a one to one.

Good Luck

Mark
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic