简体   繁体   English

Hibernate通过一对多映射和Oracle Sequence在表中插入空值

[英]Hibernate inserting null values in table with One To Many mapping and Oracle Sequence

I have two tables Employee and Department where I am trying to persist Department data through OneToMany mapping. 我有两个表Employee和Department,我试图通过OneToMany映射保留Department数据。 When i do save data is inserting into Department table and Employee table but the deptid in Employee table updating with null value. 当我保存数据时,会将数据插入到Department表和Employee表中,但是Employee表中的deptid更新为空值。 Can any one Please help me on this? 有人可以帮我吗?

Below are my classes,mapping files and configuration file. 下面是我的类,映射文件和配置文件。

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();


    }

}

Department mapping file: 部门映射文件:

 <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>

Employee mapping file: 员工映射文件:

 <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" ;

I believe your employee hbm.xml file requires this: 我相信您的员工hbm.xml文件需要这样做:

<many-to-one name="deptid" class="com.hibernate.OneToMany.Department" fetch="select">
     <column name="DEPTID" not-null="true" />
</many-to-one>

Otherwise, hibernate has no indication that a deptid exists in the Employee objects. 否则,休眠状态不表示Employee对象中存在deptid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM