On MySQL, I have a table
Dept and
Emp with following structures,
CREATE TABLE `test`.`dept` (
`deptid` int(10) unsigned NOT NULL,
`dname` varchar(45) NOT NULL,
PRIMARY KEY (`deptid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `test`.`emp` (
`deptid` int(10) unsigned NOT NULL,
`empname` varchar(45) NOT NULL,
`empsalary` int(10) unsigned NOT NULL,
`empid` int(10) unsigned NOT NULL auto_increment,
PRIMARY KEY (`empid`),
CONSTRAINT `deptid` FOREIGN KEY (`deptid`) REFERENCES `dept` (`deptid`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
while running i am getting folowing error
Error Message:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Inserting Record
Hibernate: insert into dept (dname) values (?)
three****org.hibernate.exception.GenericJDBCException: could not insert: [com.muru.pojo.Dept]
Hibernate: select max(empid) from Emp
four****org.hibernate.AssertionFailure: null id in com.muru.pojo.Dept entry (don't flush the Session after an exception occurs)
Murugesan one-to-many Record has scuccessfully
org.hibernate.AssertionFailure: null id in com.muru.pojo.Dept entry (don't flush the Session after an exception occurs)
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:78)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:187)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:49)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at com.muru.pojo.DeptTest.main(DeptTest.java:61)
Exception in thread "main"
Codes:
hibernate.cfg.xml
Code:
<?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>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">XYZ</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="News.hbm.xml" />
<mapping resource="Emp.hbm.xml" />
<mapping resource="Dept.hbm.xml" />
</session-factory>
</hibernate-configuration>
Emp.javaCode:
package com.muru.pojo;
public class Emp {
private Long empid;
private String empname;
private int empsalary;
public Emp(String name,int salary){
setEmpname(name);
setEmpsalary(salary);
}
public Long getEmpid() {
return empid;
}
public void setEmpid(Long empid) {
this.empid = empid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public int getEmpsalary() {
return empsalary;
}
public void setEmpsalary(int empsalary) {
this.empsalary = empsalary;
}
}
Emp.hbm.xmlCode:
<?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 package="com.muru.pojo">
<class name="Emp">
<id name="empid" column="empid">
<generator class="increment"></generator>
</id>
<property name="empname" column="empname" generated="never"></property>
<property name="empsalary" column="empsalary" type="int" generated="never"></property>
</class>
</hibernate-mapping>
Dept.javaCode:
package com.muru.pojo;
import java.util.HashSet;
import java.util.Set;
public class Dept {
private Long id;
private String dname;
private Set emps;
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public Set getEmps() {
return emps;
}
public void setEmps(Set emps) {
this.emps = emps;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
Dept.hbm.xmlCode:
<?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 package="com.muru.pojo">
<class name="Dept" table="dept">
<id name="id" column="deptid" type="java.lang.Long">
<generator class="native"></generator>
</id>
<property name="dname" column="dname" generated="never"></property>
<set name="emps" cascade="all-delete-orphan">
<key column="deptid" />
<one-to-many class="Emp" />
</set>
</class>
</hibernate-mapping>
DeptTest.javaCode:
package com.muru.pojo;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class DeptTest {
public static void main(String arg[]) {
Session session = null;
try {
// This step will read hibernate.cfg.xml and prepare hibernate for
// use
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
// Create new instance of Contact and set values in it by reading
// them from form object
System.out.println("Inserting Record");
Transaction tr = session.beginTransaction();
// this is for one-to-many unidirectional code
Dept test = new Dept();
test.setId(new Long("5"));
test.setDname("Computer Work");
Set emp = new HashSet();
try {
emp.add(new Emp("PM", 1000));
} catch (Exception e) {
System.out.println("one****" + e);
}
try {
emp.add(new Emp("MP", 1111));
} catch (Exception e) {
System.out.println("two****" + e);
}
try {
// session.save(emp);
test.setEmps(emp);
session.save(test);
} catch (Exception e) {
System.out.println("three****" + e);
}
try {
tr.commit();
} catch (Exception e) {
System.out.println("four****" + e);
}
System.out
.println("Murugesan one-to-many Record has scuccessfully");
} catch (Exception e) {
System.out.println(e);
} finally {
session.flush();
session.close();
}
}
}
I have tried with all class of generator(<generator class="..">) in dept.hbm.xml. But i won't get any solution.
Please help me on this.
Thanks in Advance.
Murugesan.