0

大家好,我是休眠的新手。当我为休眠相关主题浏览堆栈溢出时,我发现了这个

需要澄清将对象映射到数据库、注释和一对多关系

在阅读了解决方案后,我有点困惑,并开始构建确切的场景以了解多对一背后的实际机制,其中我使用了 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);

我认为发生异常是因为数据在插入其他表之前已插入作业表。请帮我找出我错过了什么?

4

4 回答 4

1

由于主实体中有两个实体,因此您需要先保存,然后在包含它们的主类中设置实体。恐怕我真的不记得函数 sesssion.save 或 session.saveOrUpdate 中的哪一个返回 bean,但使用该方法,使用 BeanUTils.copy 方法并在主类中设置 bean,一旦所有实体都完成,保存或更新主实体类。

希望这可以帮助!

于 2013-10-23T08:52:25.060 回答
1

首先尝试保存依赖实体(在您的情况下 Authorization )然后尝试保存外部实体(作业)

session.save(Authorization's object);
session.save(Job's object)
于 2013-10-23T07:34:52.603 回答
0

最后我发现了与作业类相关的问题,其余代码(类)都可以。在这里,我正在粘贴正确和修改过的 Job 实体

@Entity
@Table(name="JOB")
public class Job implements Serializable {

    private static final long serialVersionUID = 1L;

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


    /*   The problem was inside this block */


    @ManyToOne(fetch=FetchType.LAZY,cascade = CascadeType.ALL)
    @JoinColumn (name="JOB_AUTHID", nullable = false)
    private Authorization authorization;                                 

    @ManyToOne(fetch=FetchType.LAZY,cascade = CascadeType.ALL)
    @JoinColumn (name="JOB_JOBFILTERID", nullable = false)
    private JobFilteration jobfilteration;
于 2013-10-24T06:34:16.833 回答
-1

查看您要存储的对象。您尝试保存的对象似乎具有不接受空值的字段的空值。根据您的代码,所有数据库列都不可为空。这意味着您不能为这些列输入空值

于 2013-10-23T05:42:21.960 回答