关于映射对象到数据库中使用Hibernate [英] About Mapping Object To Database Using Hibernate
问题描述
您好,大家好我是新来的冬眠。而我是冲浪的堆栈溢出的休眠相关的主题,我发现这个
Hello guys i am new to hibernate. while i was surfing stack overflow for hibernate related topics i found this
<一个href=\"http://stackoverflow.com/questions/8347350/need-clarification-about-mapping-objects-to-database-annotations-and-one-to-ma\">Need澄清对象映射到数据库,注释和一对多的关系
阅读解决方案,我是一个有点困惑,并开始建立确切的情况,了解背后许多实际的机制之一,这里我使用了Oracle 11g数据库之后。当我运行我的项目Hibernate是自动生成的表和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])
我做什么是我所提到的上述议题的解决,我张贴我的实体类(S)请帮我解决这个问题。
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;
类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;
和插入数据我写了一个方法公共无效insertToDB(工作职位)
(我用同一顺序的三个类,以节省时间),然后
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);
我觉得发生了异常,因为数据被插入在工作表中的其他表(S)将之前。请帮我找出我缺少的是什么?
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?
推荐答案
最后我想通了与作业类相关的code的其余部分(类)都OK的问题。在这里,我粘贴正确和修改工作实体
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屋!