Jpa ElementCollection annotation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello, In previous articles we have seen about jpa embeddable objects. This article will take it further and explain jpa ElementCollection annotation.

JPA ElementCollection annotation.

When learning about embeddable objects in entities, you have seen that the fields of embeddable objects are stored as rows in columns of entity table.  Consider a scenario where an Employee can have one address and it can learn multiple number of courses.

  • Address is embedded into Employee entity. When you persist Employee , then fields of  Address  are stored in the columns of Employee .
  • The problem in embeddable object was that only one object could be embedded in some entity.

Now you have Course object that is Embeddable and an Employee can have multiple courses.

  • You cannot build OneToMany relationship in Employee and Course because it is an embeddable object and not an entity. It does not need to have separate identity in persistence context.
  • We just need to map multiple courses to Employee entity.
  • To solve this problem you can mark courses as the @ElementCollection

This is from javadoc.

Defines 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.

This mapping information between Employee and course needs to be stored into database in a separate table.

  • The table should have columns to represent embeddable object information.
  • The table should reference the owning entity

The database structure would be as follows.

EmployeeAndCourses

Here is an example on about jpa ElementCollection annotation.

Persistence Unit

Embeddable Course object

You can learn about embeddable objects

Employee Entity

Some points to note about this entity.

  • @TableGenerator and @GeneratedValue are used for automatic ID creation using jpa table generator.
  • JPA ElementCollection annotation specifies that the field marked with @ElementCollection refers to the collection of embeddable or basic objects.
  • @CollectionTable specifies the name of table in database in which collection information is stored for employee entity with  name = "employee_courses"  and the column which is used to refer to entity with joinColumns = @JoinColumn(name = "idemployee")

Let us test this example

Run this program and you will see data is inserted into employee_courses table

5 java standard edition Some description about java
5 jpa some description about jpa

I hope this helped understand the purpose of jpa ElementCollection annotation.

Feel free to ask questions and provided feedback.

References for further study :

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

9 thoughts on “Jpa ElementCollection annotation

  • Pingback:JPA OrderColumn Example | theJavaGeek

  • October 5, 2015 at 6:30 am
    Permalink

    hi! very good tutorial. I have a question.
    can use @ManyToOne Employee employee; in Course to be it bidirectional?

    Reply
  • December 18, 2016 at 9:39 pm
    Permalink

    Hello

    What happens if we annotate Course with entity annotation ?

    Reply
    • December 19, 2016 at 3:54 pm
      Permalink

      Whenever we mark anything with @Entity, then it must have valid database table representation. In our case, Course is supposed to be embedded object of Employee. So Course does not have database representation. If you mark it as Entity, then it must have database table.

      Reply
  • August 30, 2017 at 2:45 pm
    Permalink

    Hello. Nice article!

    Can I make @Embeddable class become immutable?

    Reply
  • October 26, 2018 at 2:39 pm
    Permalink

    Your explanation is too good. Could you please explain in the same manner the use of @MapKeyColumn?

    Reply
  • October 26, 2018 at 2:40 pm
    Permalink

    Could you explain in the same manner @MapKeyColumn?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *