我有两个表Employee和Department,我试图通过OneToMany映射来持久保存Department数据。当我保存数据时,插入到Department表和Employee表中,但Employee表中的deptid更新为null值。任何人都可以帮我这个吗?
以下是我的类,映射文件和配置文件。
public class Department {
private int deptid;
private String deptname;
private Set<Employee> empSet;
public int getDeptid() {
return deptid;
}
public void setDeptid(int deptid) {
this.deptid = deptid;
}
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
public Set<Employee> getEmpSet() {
return empSet;
}
public void setEmpSet(Set<Employee> empSet) {
this.empSet = empSet;
}
}
public class Employee {
private String empname;
private int empid;
private int salary;
private int age;
private int deptid;
private String gender;
public int getDeptid() {
return deptid;
}
public void setDeptid(int deptid) {
this.deptid = deptid;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
}
public class RunDemo {
/**
* @param args
*/
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.configure("com/hibernate/OneToMany/hibernate.cfg.xml");
SessionFactory factory = conf.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Employee emp1 = new Employee();
emp1.setEmpname("Sowmya");
emp1.setAge(26);
emp1.setGender("F");
emp1.setSalary(77600);
Employee emp2 = new Employee();
emp2.setEmpname("Raju");
emp2.setAge(29);
emp2.setGender("M");
emp2.setSalary(67600);
Employee emp3 = new Employee();
emp3.setEmpname("Shyam");
emp3.setAge(30);
emp3.setGender("M");
emp3.setSalary(37600);
Set<Employee> employeeSet = new HashSet<Employee>();
employeeSet.add(emp1);
employeeSet.add(emp3);
employeeSet.add(emp2);
Department department = new Department();
department.setDeptid(55);
department.setDeptname("TestDept");
department.setEmpSet(employeeSet);
session.save(department);
transaction.commit();
session.close();
}
}
部门映射文件:
<hibernate-mapping>
<class name="com.hibernate.OneToMany.Department" table="DEPARTMENT" schema="TEST">
<id name="deptid" type="int">
<column name="deptid" precision="5" scale="0" />
<generator class="assigned" />
</id>
<set name="empSet" table="EMPLOYEE" cascade="all">
<key column="deptid"></key>
<one-to-many class="com.hibernate.OneToMany.Employee"/>
</set>
<property name="deptname" length="50" type="string" column="deptname"></property>
</class>
</hibernate-mapping>
员工映射文件:
<hibernate-mapping>
<class name="com.hibernate.OneToMany.Employee" table="EMPLOYEE" schema="TEST">
<id name="empid" type="int">
<column name="empid" precision="5" scale="0" />
<generator class="sequence">
<param name="sequence">EMPID_SEQ</param>
</generator>
</id>
<property name="empname" length="50" type="string" column="empname"></property>
<property name="salary" type="int" column="salary"></property>
<property name="age" type="int" column="age"></property>
<property name="gender" length="2" type="string" column="gender"></property>
</class>
</hibernate-mapping>
CREATE TABLE "TEST"."EMPLOYEE"
( "EMPID" NUMBER NOT NULL ENABLE,
"EMPNAME" VARCHAR2(20 BYTE),
"DEPTID" NUMBER,
"SALARY" NUMBER,
"AGE" NUMBER,
"GENDER" VARCHAR2(20 BYTE),
"VER" NUMBER,
"EMPLOYMENT_TYPE" VARCHAR2(50 BYTE),
"CODE" VARCHAR2(1 BYTE),
CONSTRAINT "EMPLOYEE_PK" PRIMARY KEY ("EMPID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS" ENABLE,
CONSTRAINT "EMPLOYEE_FK1" FOREIGN KEY ("DEPTID")
REFERENCES "TEST"."DEPARTMENT" ("DEPTID") ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS" ;
CREATE TABLE "TEST"."DEPARTMENT"
( "DEPTID" NUMBER NOT NULL ENABLE,
"DEPTNAME" VARCHAR2(50 BYTE),
CONSTRAINT "DEPARTMENT_PK" PRIMARY KEY ("DEPTID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS" ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS" ;
答案 0 :(得分:0)
我相信您的员工hbm.xml文件需要这样:
<many-to-one name="deptid" class="com.hibernate.OneToMany.Department" fetch="select">
<column name="DEPTID" not-null="true" />
</many-to-one>
否则,hibernate没有迹象表明deptid
个对象中存在Employee
。