-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate OneToMany Bug ?? is it
PostPosted: Mon Jul 21, 2014 11:54 am 
Newbie

Joined: Mon Jul 21, 2014 11:45 am
Posts: 1
I was working with Hibernate and OneToMany Relationship, i came across a weird behavior today , although i did it purposely but i was not expecting this behavior

College.java

Code:
@Entity
@Table(name="COLLEGE")
public class College implements Serializable{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="COLLEGE_XID")
    private Long collegeId;

    @Column(name="COLLEGE_NAME")
    private String collegeName;

    @OneToMany(mappedBy="college",cascade = CascadeType.ALL)
    private List<Student> studentList = new ArrayList<Student>();
    //... with get and setters
}


Student.java
Code:
@Entity
@Table(name = "STUDENT")
public class Student implements Serializable    {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="STUDENT_XID")
    private Long studentId;

    @Column(name="STUDENT_NAME")
    private String studentName;

    @ManyToOne
    @JoinColumn(name="COLLEGE_XID",nullable=false)
    private College college;
    //... with get and setters
}


Application.java
Code:
College college1 = new BdCollege();
    college1.setCollegeName("C_1");

    College college2 = new BdCollege();
    college2.setCollegeName("C_2");

    Student student1 = new BdStudent();
    student1.setStudentName("S_1_C_2");

    CollegeDAO collegeDAO = new  CollegeDAO();

    // setting student 1 Parent to College 1       
    student1.setCollege(college1);   // <---- Here the Issue  ... student 1 as college 1 child

    // BUT assigning Student 1 as Child of Collge 2 in List
    // i did it purposely to test the behaviour
    college2.getStudentList().add(student1);  // <---- Here the Issue ... but assigning it in the list of College 2 ... ws expecting Exception

     // Saved College 1 & 2
     collegeDAO.save(college1);
     collegeDAO.save(college2);

      // Saved Successfully BUT


RESULT COLLEGE Table
Code:
COLLEGE_XID     COLLEGE_NAME   
--------------  ---------------
1               C_1             
2               C_2


STUDENT Table
Code:
STUDENT_XID     STUDENT_NAME     COLLEGE_XID   
--------------  ---------------  --------------
1               S_1_C_2          1


I was expecting some exception as assignment from child and parents mis-matched in college - student example and another thing that stucked me were the query generated by hibernate

Quote:
Hibernate: insert into COLLEGE (COLLEGE_NAME) values (?)
Hibernate: insert into COLLEGE (COLLEGE_NAME) values (?)
Hibernate: insert into STUDENT (COLLEGE_XID, STUDENT_NAME) values (?, ?)


i.e it save College 1 first, Then College 2 Second and Saving Student as Child of College 2 but in the result it appears as child of College 1


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.