如何在JPA中保存或更新值为Null的OneToMany列(使用Play框架)

时间:2016-08-11 03:52:16

标签: java entity-framework hibernate jpa playframework-2.0

我目前正在开发一个使用PLAY框架和JPA的系统。我的问题是,如果Department_id(连接到另一个Entity类)为Null或没有值,我就无法保存EMPLOYEE。

这是我的员工实体类

 @Entity
 public class Employees{

        @Id
        public int employee_id;
        public String first_name;
        public String last_name;
        public String email;
        public String phone_number;
        public java.sql.Date hire_date;
        public String salary;
        public String commission_pct;

        @ManyToOne
        @JoinColumn(name="department_id",nullable = true)
        private Departments department_id;

        @ManyToOne
        @JoinColumn(name="job_id")
        private Jobs job_id;

        @ManyToOne
        @JoinColumn(name="manager_id",nullable = true)
        private Employees manager_id;
        @OneToMany(mappedBy = "manager_id")
        Set<Employees> emps = new HashSet<Employees>();
    }

这是我的部门实体类。

@Entity
public class Departments {

    @Id
    private String department_id;
    private String department_name;
    private int manager_id;
    @OneToMany(mappedBy="department_id")
    private Set<Employees> emps = new HashSet<Employees>();
}

这是错误

Caused by: org.hibernate.TransientObjectException: object references an unsaved
transient instance - save the transient instance before flushing: modelsDomain.E
mployees.department_id -> modelsDomain.Departments
        at org.hibernate.engine.CascadingAction$9.noCascade(CascadingAction.java
:387) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.engine.Cascade.cascade(Cascade.java:172) ~[hibernate-co
re-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(
AbstractFlushingEventListener.java:154) ~[hibernate-core-3.6.9.Final.jar:3.6.9.F
inal]
        at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFl
ushes(AbstractFlushingEventListener.java:145) ~[hibernate-core-3.6.9.Final.jar:3
.6.9.Final]
        at org.hibernate.event.def.AbstractFlushingEventListener.flushEverything
ToExecutions(AbstractFlushingEventListener.java:88) ~[hibernate-core-3.6.9.Final
.jar:3.6.9.Final]
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlus
hEventListener.java:50) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]

0 个答案:

没有答案