代码之家  ›  专栏  ›  技术社区  ›  NaN

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

  •  0
  • NaN  · 技术社区  · 11 年前

    大家好,我刚开始冬眠。当我浏览hibernate相关主题的堆栈溢出时,我发现了这个

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

    在阅读了解决方案后,我有点困惑,并开始构建确切的场景,以了解多对一背后的实际机制,在那里我使用了oracle11g数据库。当我运行我的项目时,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;
    

    类作业筛选

    @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 回复  |  直到 7 年前
        1
  •  1
  •   Ramesh    11 年前

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

    session.save(Authorization's object);
    session.save(Job's object)
    
        2
  •  1
  •   DSS    11 年前

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

    希望这能有所帮助!

        3
  •  0
  •   Josh Crozier HBP    11 年前

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

    @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;
    
        4
  •  -1
  •   Chaminda Hettigoda    11 年前

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