大家好,我是休眠的新手。当我为休眠相关主题浏览堆栈溢出时,我发现了这个
在阅读了解决方案后,我有点困惑,并开始构建确切的场景以了解多对一背后的实际机制,其中我使用了 oracle 11g 数据库。当我运行我的项目时,休眠会自动生成表和 FK 关系,但是在插入数据时它面临这个问题(这很明显)
WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
Unsaved transient entity: ([com.entity.Authorization#0])
Dependent entities: ([[com.entity.Job#1]])
WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
Unsaved transient entity: ([com.entity.JobFilteration#0])
Dependent entities: ([[com.entity.Job#1]])
Non-nullable association(s): ([com.entity.Job.jobfilteration])
我确实做了我提到的上述主题的解决方案,我正在发布我的实体类请帮我解决这个问题
班级工作
@Entity
@Table(name="JOB")
public class Job implements Serializable {
@Id
@SequenceGenerator(name="person_seq", sequenceName="SEQ_PERSON",initialValue=1,allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE ,generator="person_seq")
@Column(name="JOB_SERIAL")
private int id;
@Column(name="JOB_CATAGORY",nullable=false,length=200,unique=true)
private String catagory;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn (name="JOB_AUTHID", nullable = false, updatable = false, insertable = false)
private Authorization authorization;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn (name="JOB_JOBFILTERID", nullable = false, updatable = false, insertable = false)
private JobFilteration jobfilteration;
类 JobFilteration
@Entity
@Table(name="JOB_FILTERATION")
public class JobFilteration implements Serializable {
@Id
@SequenceGenerator(name="person_seq", sequenceName="SEQ_PERSON",initialValue=1,allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE ,generator="person_seq")
@Column(name="FILTER_SERIAL")
private int id;
@Column(name="FILTERNAME",nullable=false,length=200)
private String filtername;
班级授权
@Entity
@Table(name="AUTHORIZATION")
public class Authorization implements Serializable {
@Id
@SequenceGenerator(name="person_seq", sequenceName="SEQ_PERSON",initialValue=1,allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE ,generator="person_seq")
@Column(name="AUTH_ID")
private int id;
@Column(name="AUTHORISEDBY",nullable=false,length=200)
private String authorisedby;
并且为了插入数据我写了一个方法 public void insertToDB(Job job)
(我对三个类使用相同的序列以节省时间)然后
JoBDao jobdao= new JoBDao();
Job job= null;
Authorization auth = null;
JobFilteration jfilter = null;
try{
job= new Job();
auth = new Authorization();
auth.setAuthorisedby("SOMEPERSON");
jfilter = new JobFilteration();
jfilter.setFiltername("JEE");
job.setCatagory("PERMANENT");
job.setAuthorization(auth);
job.setJobfilteration(jfilter);
jobdao.addPersonandCard(job);
我认为发生异常是因为数据在插入其他表之前已插入作业表。请帮我找出我错过了什么?