为什么只能插入一个表(hibernate 一对多)
//Job.java
public class Job {
private Integer job_id;
private String job_desc;
private Set emp=new HashSet();
public Integer getId(){
return this.job_id;
}
public void setId(Integer job_id){
this.job_id=job_id;
}
public String getJobDesc(){
return this.job_desc;
}
public void setJobDesc(String job_desc){
this.job_desc=job_desc;
}
public Set getEmp(){
return this.emp;
}
public void setEmp(Set emp){
this.emp=emp;
}
}
//////////////////////////////////////////////////////////////////////////////
Emp.java
public class Emp {
private Integer emp_id;
private String name;
private Integer jobid;
private Job job;
public Integer getId(){
return this.emp_id;
}
public void setId(Integer emp_id){
this.emp_id=emp_id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name=name;
}
public Integer getJobId(){
return this.jobid;
}
public void setJobId(Integer jobid){
this.jobid=jobid;
}
public Job getJob(){
return this.job;
}
public void setJob(Job job){
this.job=job;
}
}
///Job.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="lht.hibernate">
<class
name="Job"
table="job"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="Id"
type="integer"
column="job_id"
>
<generator class="vm"/>
</id>
<property
name="JobDesc"
column="job_desc"
type="string"
not-null="false"
length="32"
/>
<set
name="emp"
table="emp"
lazy="false"
inverse="true"
cascade="all"
sort="unsorted"
>
<key
column="job_id"
>
</key>
<one-to-many
class="lht.hibernate.Emp"
/>
</set>
</class>
</hibernate-mapping>
///Emp.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="lht.hibernate">
<class
name="Emp"
table="emp"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="Id"
type="integer"
column="emp_id"
>
<generator class="vm"/>
</id>
<property
name="Name"
column="name"
type="string"
not-null="false"
length="32"
/>
<property
name="JobId"
column="job_id"
type="integer"
not-null="false"
length="11"
/>
<many-to-one
name="job"
class="lht.hibernate.Job"
cascade="save-update"
outer-join="auto"
update="false"
insert="false"
access="property"
column="job_id"
/>
</class>
</hibernate-mapping>
运行Demo.java
public class Demo {
public static void main(String[] args) throws HibernateException {
Job job1=new Job();
job1.setId(new Integer(1));
job1.setJobDesc("工程师");
Job job2=new Job();
job2.setId(new Integer(2));
job2.setJobDesc("程序员");
Emp emp1=new Emp();
emp1.setId(new Integer(1));
emp1.setName("张三");
emp1.setJob(job1);
Emp emp2=new Emp();
emp2.setId(new Integer(2));
emp2.setName("李四");
emp2.setJob(job1);
Emp emp3=new Emp();
emp3.setId(new Integer(3));
emp3.setName("王二");
emp3.setJob(job2);
job1.getEmp().add(emp1);
job1.getEmp().add(emp2);
job2.getEmp().add(emp3);
Configuration cfg=new Configuration();
SessionFactory sf=cfg.configure().buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
session.save(job1);
session.save(job2);
tx.commit();
session.close();
sf.close();
}
}
运行结果 job表里插入了两条记录,而emp里没插进去,jvm丢出以下异常:
......
Hibernate: insert into job (job_desc, job_id) values (?, ?)
Hibernate: insert into job (job_desc, job_id) values (?, ?)
Hibernate: update emp set name=?, job_id=? where emp_id=?
Hibernate: update emp set name=?, job_id=? where emp_id=?
Hibernate: update emp set name=?, job_id=? where emp_id=?
(impl.SessionImpl 2400) Could not synchronize database state with session
以下是异常部分
net.sf.hibernate.HibernateException: Batch update row count wrong: 0
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:65)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:128)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2393)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2261)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at lht.hibernate.Demo.main(Demo.java:57)
Exception in thread "main"
这是什么问题呢?