问题
Hello guys i am new to hibernate. while i was surfing stack overflow for hibernate related topics i found this
Need clarification about mapping objects to database, annotations, and one to many relationships
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
Class Job
@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;
Class Authorization
@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;
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?
回答1:
First try to save Dependent Entity (in your case Authorization ) then try to save Outer Entity (Job)
session.save(Authorization's object);
session.save(Job's object)
回答2:
Since you have two entities inside the main one, u need to save then first and then set the entities in the main class that contains them. I am afraid i really dont recall which one of the functions sesssion.save or session.saveOrUpdate returns the bean, but use that method, use BeanUTils.copy method and set the bean inside the main class, once that is dne for all the entities, saveOrUpdate the main entity class.
Hope this helps!
回答3:
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;
回答4:
see the object you are going to store. Seem object you try to save have null values for the field those not accept null values. According to you code all the DB columns are not nullable. that mean you can't enter null values for those columns
来源:https://stackoverflow.com/questions/19533413/about-mapping-object-to-database-using-hibernate