0

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?

4 Answers 4

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)
Sign up to request clarification or add additional context in comments.

4 Comments

For that i have to use manytoone in Authorization entity right??
i got that point the reason of exception and i already mentioned in this post but my question is why even when we use manytoone relation ship forget about three tables lets just talk about two tables or two entity class we do the same thing right? then whats the problem in my code?
jobdao.addPersonandCard(job); Before to that try to save auth and jfilter objects and save job Else try to add cacade all in many to one annotation @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
ramesh you are partly true actually i have figured out the problems ya not putting the cascade = CascadeType.ALL was one of the reason(s) behind the exception
1

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 Comments

no my point was either the thread i mentioned above was incorrect or i am doing something wrong i just want to know what I am missing here?
well i guess there is nothing wrong with the annotations, but then if u have the entities set as non-nullable then when u attempt to just save the main entity the error will be thrown, because hibernate would attempt to check if the corresponding entry (associated with the FK exists or not), and since it is nt defined at all or probably nt saved first, the id wud ofcz be empty so then the error i presume..
@Rat-a-tat-a-tatRatatouille : Could you elaborate via some code snippet on how to save corresponding entities before saving main entity. I m facing similar problem and also I have bidirectional mappings.
0

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;

Comments

-1

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

1 Comment

I have filled every value there, now i have submitted all the classes please have a look

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.