Home » Java » java – Can we created @CollectionTable inside @Entity class which class is used as CollectionTable in some other @Entity class-Exceptionshub

java – Can we created @CollectionTable inside @Entity class which class is used as CollectionTable in some other @Entity class-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have @Entity class as

@Entity
@Table(name = "EMPLOYEE")
public class Emplyee
{
  @Id
  @Column(name = "ID", length = 50)
  private String id;

  @Column(name = "ID", length = 50)
  private String name;

  @CollectionTable(name = "Address", joinColumns = @JoinColumn(name = "ID"), 
      indexes = @Index(name = "Address_FK_IDX", columnList = "ID"), 
      foreignKey = @ForeignKey(name = "EMPLOYEE_Address_FK"))
  @Column(name = "EXPORT_FORMAT", length = 50)
  private List<Address> address;

}

I want to create Address entity which will hold below attributes

String addressLine; 
String city;
String pinCode;
List<String> phoneNo;

In this entity class I want to store list of phoneNo inside another collection table.
Can we do this using @CollectionTable or is there any other way to do this?

How to&Answers:

You can do this as below. CollectionTableis normally used for collections of embeddable items rather than entities. You’d need to read up on the difference. While there is a good case for making Address an embeddable here rather than an entity, that would cause issues with the phone numbers so we make Address an Entity mapped to Employee using @OneToManyand use ElementCollection within Address for phone numbers.

@Entity
@Table(name = "EMPLOYEE")
public class Employee
{
  @Id//<-need to specify a generator here 
  @Column(name = "ID", length = 50)
  private String id;

  @Column(name = "ID", length = 50)
  private String name;

  @OneToMany(cascade = CascadeType.ALL)
  //define FK column in Address table
  @JoinColumn(joinColumns = @JoinColumn(name = "EMPLOYEE_ID"), 
      foreignKey = @ForeignKey(name = "EMPLOYEE_Address_FK"))
  private List<Address> address;
}

Address:

@Entity
@Table("Address")
public class Address {
    @Id //<-need to specify a generator here 
    private Long id;
    String addressLine; 
    String city;
    String pinCode;

    @ElementCollection
    @CollectionTable(name = "Phone", joinColumns = @JoinColumn(name = "ADDRESS_ID"), foreignKey = @ForeignKey(name = "PHONE_Address_FK"))
    List<String> phoneNo;
}

As noted above, @CollectionTable is normally used with collections of embeddables and while there is a case for making Address an @Embeddable rather than an @Entity that would cause issues as the JPA spec (2.6) notes:

An embeddable class (including an embeddable class within another
embeddable class) that is contained within an element collection must
not contain an element collection, nor may it contain a relationship
to an entity other than a many-to-one or one-to-one relationship. The
embeddable class must be on the owning side of such a relationship and
the relationship must be mapped by a foreign key mapping. (See Section
2.9.)