-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate JPA is not saving data into Join table
PostPosted: Sun May 06, 2012 2:38 am 
Newbie

Joined: Sun May 06, 2012 12:47 am
Posts: 3
Following are the entities and their relationships in my system

Entities:
Code:
1. Certification
2. Group
3. Exam


Relationships:
Code:
1. One Certification can have multiple Groups (One-to-Many)
2. One Group can have multiple Exams and one Exam can belong to multiple Groups (Many-to-Many)
3. Exam data is created through a separate use case
4. While creating Certifications, the user can add new Groups and associate pre-exising Exams.


I have created following tables for this:
Code:
1. demo_cert
2. demo_grp (it has a FK to demo_cert's ID column)
3. demo_exam
4. demo_exam_grp(group_id, exam_id)


Following code snippets show the Domain object definitions

Code:
@Entity
@Table(name="demo_nn_cert")
@Access(AccessType.FIELD)
public class DemoCert
{
        @Id
   @Column(name="ID")
   @GeneratedValue(strategy=GenerationType.AUTO, generator="CERT_SEQ")
   @SequenceGenerator(name="CERT_SEQ", sequenceName="demo_cert_seq")
   private Long id;
   
   @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, orphanRemoval=true, mappedBy="demoCert", targetEntity=DemoGrp.class)
   private Set<DemoGrp> groups;
   
   @Version
   private long version;
   
   // -- Other properties and their getter/setters
}

Code:
@Entity
@Table(name="demo_nn_grp")
@Access(AccessType.FIELD)
public class DemoGrp implements Serializable
{
   /**
    *
    */
   private static final long serialVersionUID = 7438445502527409879L;

   @Id
   @Column(name="ID")
   @GeneratedValue(strategy=GenerationType.AUTO, generator="GROUP_SEQ")
   @SequenceGenerator(name="GROUP_SEQ", sequenceName="demo_group_seq")
   private Long id;
      
   @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY, targetEntity=DemoCert.class)
   @JoinColumn(name="CERT_ID")
   private DemoCert demoCert;
   
   @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, orphanRemoval=true, targetEntity=DemoExam.class)
   @JoinTable
   (
      name="DEMO_EXAM_GRP",
      joinColumns={@JoinColumn(name="GROUP_ID", referencedColumnName="ID")},
      inverseJoinColumns={@JoinColumn(name="EXAM_ID", referencedColumnName="ID")}
   )
   private Set<DemoExam> exams;
   
   // -- Other properties and getters/setters
}

Code:
@Entity
@Table(name="demo_nn_exam")
@Access(AccessType.FIELD)
public class DemoExam implements Serializable
{
   /**
    *
    */
   private static final long serialVersionUID = 375151244325884919L;

   @Id
   @Column(name="ID")
   private long id;
   
   @Version
   private long version;
   
   // -- Other properties and getters/setters
}


To test, I am creating the entire object graph and call
Code:
EntityManager.persist(demoCert)
.

The issue I am facing is, instead of inserting data to join table demo_exam_grp, it is trying to insert into demo_exam table and failing because the data is already present.

Could someone help me?

I am using

- Spring 3.1.1 (accessing JPA through Spring-Data module)
- Hibernate 4.1.1


Top
 Profile  
 
 Post subject: Re: Hibernate JPA is not saving data into Join table
PostPosted: Mon May 07, 2012 12:31 am 
Newbie

Joined: Sun May 06, 2012 12:47 am
Posts: 3
Is this an active forum?


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

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.