Announcement Announcement Module
Collapse
No announcement yet.
Spring-Data Hibernate JPA is not saving data into Join table Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring-Data Hibernate JPA is not saving data into Join table

    Following are the entities and their relationships in my system

    Entities:
    Code:
    1. Certification
    2. Group
    3. Exam
    Relationship:
    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
    Code:
    demo_exam_grp
    , it is trying to insert into [code]demo_exam[c/ode] 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
Working...
X