关于使用 Hibernate 将对象映射到数据库 [英] About Mapping Object To Database Using Hibernate

查看:23
本文介绍了关于使用 Hibernate 将对象映射到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello guys i am new to hibernate. while i was surfing stack overflow for hibernate related topics i found this

需要说明映射对象到数据库、注释和一对多关系

阅读解决方案后,我有点困惑,并开始构建确切的场景以了解多对一背后的实际机制,其中我使用了 oracle 11g 数据库.当我运行我的项目时,休眠会自动生成表和 FK 关系,但在插入数据时它面临这个问题(这很明显)

after reading the solution i was a bit confused and started building the exact scenario to understand the actual mechanism behind many to one, where i used oracle 11g database. When i am running my project hibernate is generating the tables and FK relations automatically but in the time of inserting data it facing this problem (which is obvious)

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])

我完全按照我提到的上述主题的解决方案做了什么,我正在发布我的实体类,请帮我解决这个问题

i did exactly what was the solution of the above topic I have mentioned, I am posting my entity class(s) Please help me to solve this

课堂作业

@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)(我对三个类使用了相同的序列以节省时间)然后

and for inserting the data i wrote a method public void insertToDB(Job job)(I used same sequence for three classes to save time) and then

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

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

I think the exception occurred because the data was inserted in the job table before inserting in the other table(s). Please help me to find out what am I missing?

推荐答案

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

Finally i have figured out the problems associated with the job class the rest of the code(classes) are OK. here i am pasting the correct and modified Job entity

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

这篇关于关于使用 Hibernate 将对象映射到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆