关于使用Hibernate将对象映射到数据库

时间:2013-10-23 05:24:36

标签: java hibernate oracle11g annotations persistence

大家好,我是hibernate的新手。当我在hibernate相关主题上冲浪堆栈溢出时,我发现了这个

  

Need clarification about mapping objects to database, annotations, and one to many relationships

阅读解决方案后,我有点困惑,并开始构建确切的场景,以了解多对一背后的实际机制,我使用oracle 11g数据库。当我运行我的项目时,hibernate会自动生成表和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;

Class 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 个答案:

答案 0 :(得分:1)

首先尝试保存从属实体(在您的情况下授权)然后尝试保存外部实体(作业)

session.save(Authorization's object);
session.save(Job's object)

答案 1 :(得分:1)

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

希望这有帮助!

答案 2 :(得分: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;

答案 3 :(得分:-1)

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