@ElementCollection Example in Hibernate/JPA Using Spring Boot

In this tutorial, we will see the @ElementCollection Example in Hibernate/JPA Using Spring Boot and Oracle.

Understanding @ElementCollection annotation.

We will see how to use @ElementCollection annotation to map a collection of a basic type(String, Integer etc) and embeddable types(User Defined class for example Book.java).

Consider we have an entity called Student.java. A student can have a list of book and list of a phone number. Let’s see how to map classes using @ElementCollection, @CollectionTable and @Embeddable annotation.

For bookList we will define the Book.java class as @Embeddable because we may need to define some other information like bookName, author, numberOfPage etc. For the phoneNumbers we might want just phone number so we will define this one basic type(for example String). We will see more details about @ElementCollection, @CollectionTable and @Embeddable annotations at the end of the tutorial. Let’s see how to use @ElementCollection with embeddable types or Basic types.

Book.java

 

Student.java

 

Observe both classes. Did you notice that with Book.java we are using @Embeddable annotation, we are not using @Entity annotation.

 

Before moving forward let’s see what we are going to do in this example.

We will have two REST endpoint to save and retrieve data.

POST – http://localhost:9091/student/save

@ElementCollection Example in Hibernate/JPA Using Spring Boot

 

http://localhost:9091/student/allstudents

 

Database entry and structure after saving the record.

 

Note – In the above diagram for student_book and student_phone_number table, the student_id is the foreign key. We are not going to create all these tables manually, let’s hibernate do this job(we will have configurations in application.properties file).

 

@ElementCollection Example in Hibernate Using Spring Boot and Oracle. 

Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’ click on next.  Fill all details(GroupId – elementcollection, ArtifactId – elementcollection and name – elementcollection) and click on finish. Keep packaging as the jar.

Modify pom.xml

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

If you see any error for oracle dependency then follow these steps.

Let’s see the directory structure of  @ElementCollection Example in Hibernate/JPA Using Spring Boot below.

Directory structure –

@ElementCollection Example in Hibernate/JPA Using Spring Boot

 

Entity for @ElementCollection Example in Hibernate/JPA Using Spring Boot.

Student.java

 

Book.java

 

StudentController.java

We have seen controller and serviceimpl, interface [email protected] Example in Hibernate/JPA Using Spring Boot

Note – See more details about @Controller and RestController here.

StudentService.java – interface

Note – See here more about @Component, @Controller, @Service and @Repository annotations here.

StudentServiceImpl.java

SpringMain.java

Note – See more details about @ComponentScan here.

Configuration file @ElementCollection Example in Hibernate/JPA Using Spring Boot.

JpaConfig.java

Note – See more details about @Configuration annotations here.

application.properties

Let’s run the SpringMain class(run as java application).

Perform save operation first using below REST API.

POST – http://localhost:9091/student/save

 

Response Data.

Let’s perform GET call for @ElementCollection Example in Hibernate/JPA Using Spring Boot.

GET – http://localhost:9091/student/allstudents

Below queries will get fired while performing get operation.

Hibernate: select student0_.id as id1_0_, student0_.roll_number as roll_number2_0_, student0_.student_name as student_name3_0_ from student student0_
Hibernate: select phonenumbe0_.student_id as student_id1_2_0_, phonenumbe0_.phone_numbers as phone_numbers2_2_0_ from student_phone_number phonenumbe0_ where phonenumbe0_.student_id=?
Hibernate: select booklist0_.student_id as student_id1_1_0_, booklist0_.author as author2_1_0_, booklist0_.book_name as book_name3_1_0_, booklist0_.number_of_page as number_of_page4_1_0_ from student_book booklist0_ where booklist0_.student_id=?

 

Difference between @ElementCollection and @OneToMany annotation.

Both @ElementCollection and @OneToMany annotation used with collection type of fields. First, see some basic difference between these annotations later we will see when we should use @ElementCollection.

The @ElementCollection annotation introduced in JPA 2.0 whereas @OneToMany introduced in JPA 1.0.

In the case of the @ElementCollection, child entity(Book.java) need to annotate with @Embeddable annotation while in case of @OneToOne child class need to annotate with @Entity. The @ElementCollection has only two elements targetClass and fetch, @OneToMany annotation contains some other elements like cascade, mappedBy, orphanRemoval etc.

If we have @ElementCollection type filed then target objects cannot be queried, persisted, merged independently of their parent object. Since they are tightly dependent on the parent entity. In the case of @OneToMany, the child entity can be independently queried, persisted or merged.

The @ElementCollection is used to map non-entities (embeddable or basic) while @OneToMany is used to map entities. We need to use @Embeddable annotation to make an embeddable class(generally @Embeddable used with User defined class). When we say basic types that means String or any other wrapper classes.

The embeddable classes totally depend on the owner entity. Without an owner entity, there is no existence of embeddable classes. But in the case of @OneToMany annotation, all entities have an independent existence. In the case of OneToMany, even parent/owner entity doesn’t exist child entity can exist. But If we use @ElementCollection it is not possible.

Conclusion – Use @OneToMany annotation for aggregation kind of relationship and use @ElementCollection for composition kind of relationship.

 

Some more Basic points about @ElementCollection, @CollectionTable and @Embeddable annotations.

@ElementCollection.

  • This annotation is introduced in JPA 2.0 and available in javax.persistence package.
  • This annotation can be used with method or field.
  • It contains two elements fetch and targetClass. we have already used fetchType in our example. Both elements are optional.
  • Before introducing @ElementCollection, hibernate had similar kind of annotation @CollectionOfElements(see docs here) which has been deprecated.

@Embeddable.

  • This annotation is introduced in JPA 1.0 and available in javax.persistence package.
  • This annotation can be used with the class only.

@CollectionTable.

  • This annotation is introduced in JPA 2.0 and available in javax.persistence package.
  • This annotation can be used with method or field.
  • It contains below elements.

 

That’s all about @ElementCollection Example in Hibernate/JPA Using Spring Boot and Oracle.

You may like.

@ElementCollection docs.

Summary – We have seen about @ElementCollection Example in Hibernate/JPA.

Top