Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ElementCollection @CollectionTable in One to Many mapping

I am trying relationship between in JPA using Embedded annotation, but i am not able to run it successfully,

Here my Database sql Script is as follows,

create table TBL_COLLEGE(
   id integer primary key generated always as identity (start with 1000, increment by 5),
   name varchar(50)
)

create table TBL_COURSE(
   Id  integer primary key generated always as identity (start with 10, increment by 1),
   college_Id integer references TBL_COLLEGE,
   name varchar(50)
)

here is the code below for JPA,

@Embeddable
public class Course {
...
...
..
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="ID")
private Integer courseId;

@Column(name="NAME")
private String courseName;

@Column(name="COLLEGE_ID")
private Integer collegeId;
....
// getter and setter
}

Here is the College mapping,

@Entity
@Table(name="TBL_COLLEGE")
public class College implements Serializable{
   @Id
   @GeneratedValue(strategy= GenerationType.IDENTITY)
   @Column(name="ID")
   private Integer collegeId;

   ...
   ..
   @ElementCollection(targetClass=Course.class,fetch= FetchType.LAZY)
   @CollectionTable(name="TBL_COURSE",joinColumns=@JoinColumn(name="COLLEGE_ID"))
   private Set<Course> course;
   ..
   // getter and setter
}

But if i try to persist College with Courses collection set, it gives me an exception,

ERROR: HCANN000002: An assertion failure occurred (this may indicate a bug in Hibernate)
org.hibernate.annotations.common.AssertionFailure: Declaring class is not found in the inheritance state hierarchy: com.entities.Course

....
..

Can you please tell me whether my approach is wrong, Or my understanding for @CollectionTable is still minimal, Were i am going wrong

like image 791
Rahul Shivsharan Avatar asked Mar 14 '13 12:03

Rahul Shivsharan


People also ask

What is the difference between @ElementCollection and @OneToMany?

The limitations of using an ElementCollection instead of a OneToMany is that the target objects cannot be queried, persisted, merged independently of their parent object. They are strictly privately owned (dependent) objects, the same as an Embedded mapping.

What is the difference between @ElementCollection and OneToMany Can they be used interchangeably?

I believe @ElementCollection is mainly for mapping non-entities (embeddable or basic) while @OneToMany is used to map entities. So which one to use depend on what you want to achieve.

What is @ElementCollection in hibernate?

Annotation Type ElementCollectionDefines a collection of instances of a basic type or embeddable class. Must be specified if the collection is to be mapped by means of a collection table. Example: @Entity public class Person { @Id protected String ssn; protected String name; ...

What is @CollectionTable?

In the simplest term, @ElementCollection tells the compiler that we are mapping a collection, in which, @CollectionTable gives the name of the target table and then @JoinColumn specifies the actually column we join on like below: 1`


1 Answers

Because both of your tables have their own ID columns, Hibernate will want them to both be @Entity types. @Embeddables do not have their own ID. So, first step is to change Course to be @Entity, with corresponding @TableName, etc.

This leads to the second issue, which is that the collection of Course objects shouldn't be an @ElementCollection, it should be a @OneToMany entity collection, with a @JoinColumn specifying that COLLEGE_ID will is the foreign key from TBL_COURSE.

Finally, by having a collection of Course in College, as well as a College id in Course, you are implying that you want a bidirectional association. Among other impacts of that, you should not have the ID of the college in Course. You should simply have the College reference. If you don't need to navigate from Course->College, you may want to remove that for now, until you've gained a better understanding of Hibernate's object mapping.

like image 138
sharakan Avatar answered Oct 10 '22 22:10

sharakan