【发布时间】:2014-12-15 13:16:30
【问题描述】:
我正在使用 jpa 和 play framework。我有一个实体 JobseekerDetails
@Entity
public class JobseekerDetails {
@Id
@Column(name = "jobseekerDetails_id", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long jobseekerDetails_id;
@ManyToOne
@JoinColumn(name = "basicEducation_id")
private JobseekerFormBasicEducation basicEducation;
@ManyToOne
@JoinColumn(name = "masterEducation_id")
private JobseekerFormMasterEducation masterEducation;
@ManyToOne
@JoinColumn(name = "doctrateEducation_id")
private JobseekerFormDoctrateEducation doctrateEducation;
@ElementCollection
private List<String> certificateName =new ArrayList<String>();
@Column(length = 3000)
private String resume;
private Long experience;
private String skills;
private String resumePath;
@ManyToOne
@JoinColumn(name = "industry_id")
private JobseekerFormIndustry industry;
@ManyToOne
@JoinColumn(name = "functionalArea_id")
private JobseekerFormFunctionalArea functionalArea;
}
与 JobseekerFormFunctionalArea 、 JobseekerFormIndustry 等其他实体具有多对一关系。这些实体具有已保存在数据库中的固定值。
当 JobseekerDetails 被保存时,它的所有详细信息都应与对应的多对一关系 id 一起保存,但不要保存到实体 JobseekerFormFunctionalArea 和 JobseekerFormIndustry,因为它们是预定义的
我的问题是,当我(通过我的表单)保存 JobseekerDetails 中的所有多对一关系字段 ID 时,它会正确保存,但是当提交我的表单时没有在任何多对一关系字段中选择任何值例如,如果我没有在我的fuctionalArea_id 字段它给出了以下异常
org.hibernate.TransientObjectException:object references an unsaved transient instance - save the transient instance before flushing
如果我选择所有字段,详细信息将被保存,但如果我选择在我的模型中映射为 manytoone 的任何字段,那么它会给出上述异常
但是 jpa 自动设置 nullable=true 那么为什么会发生这种情况
我搜索了一下,发现这个问题可以通过添加级联来解决。我添加了级联类型合并,但得到了同样的异常。
我也尝试设置 nullable=true 但得到同样的错误
在设置 cascade = CascadeType.ALL 和 cascade = CascadeType.PERSIST 我遇到了异常
PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist:
任何帮助将不胜感激
【问题讨论】:
标签: java hibernate jpa orm playframework