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