Search code examples
javajpaeclipselinkembeddable

Strange issue while configuring ID types for Embeddable class in EclipseLink-2.5.2


In my current implementation I have separate entity classes for each db table. I am using JPA along with eclipselink-2.5.2. This is working fine for me but at some point when data is huge, it lags. That's why I decided to start using @Embedded, @Embeddable and @EmbeddedId. While doing this I am getting an error which is very strange for me. Here is the full stacktrace posted: https://gist.githubusercontent.com/tjDudhatra/b955812e0d1a71cf97f1/raw/11ea458869e24baae744530417ac99bc877ed514/gistfile1.txt

Being Specific, let me give you the exact scenario in which case I am getting the exception. Consider this Code block which has three class. One is annotated as @Entity and other twos are annotated as @Embeddable. I know that in one class we cannot define @Id and @EmbeddedId and I have not done like that, then too while deploying server, I am getting the exception which only says that:

[class org.apache.{SomeClass}] has both an @EmbdeddedId (on attribute [id]) and an @Id (on attribute []. Both ID types cannot be specified on the same entity.

@Entity
@Table(name="user")  
public class User {

  @ID
  public Long id;

  @Column(name="userCode")
  public String userCode;

  @ElementCollection
  @CollectionTable(name = "address", joinColumns = @JoinColumn(name = "user_id"))
  public List<Address> addressList;

  ....
}

@Embeddable  
public class Address {

  @EmbeddedId
  @Column(name = "id")
  public Long id;

  @Column(name="userId")
  public Long userId;

  @Column(name="address-line-1")
  public String addressLine1;

  @Column(name="address-line-2")
  public String addressLine2;

  @ElementCollection
  @CollectionTable(name = "phone", joinColumns = @JoinColumn(name = "user_id"))
  protected List<Phone> phoneList;

  ....
}

@Embeddable 
public class Phone {

  @EmbeddedId
  @Column(name = "id")
  public Long id;

  @Column(name="contact_no")
  public String contactNo;

  @Column(name="country_code")
  public int countryCode;

  @Column(name="address_id")
  public int addressId;

  ....
}

Please let me know if more details required and any kind of help will be much much appreciated.

Thanks,


Solution

  • So here I found the issue. I did some analysis and got to know more about embeddable and elementCollection. Here is the reference: https://en.wikibooks.org/wiki/Java_Persistence/ElementCollection

    So now I modified the code accordingly and it's working fine now. This is how I made it work:

    @Entity
    @Table(name="user")  
    public class User {
    
      @ID
      public Long id;
    
      @Column(name="userCode")
      public String userCode;
    
      @OneToMany(fetch = FetchType.EAGER)
      @JoinColumn(name = "user_id")
      public List<Address> addressList;
    
      ....
    }
    
    @Entity
    @Table(name="address")  
    public class Address {
    
      @Id
      @Column(name = "id")
      public Long id;
    
      @Column(name="userId")
      public Long userId;
    
      @Column(name="address-line-1")
      public String addressLine1;
    
      @Column(name="address-line-2")
      public String addressLine2;
    
      @OneToMany(fetch = FetchType.EAGER)
      @JoinColumn(name = "address_id")
      protected List<Phone> phoneList;
    
      ....
    }
    
    @Entity
    @Table(name="phone")
    public class Phone {
    
      @Id
      @Column(name = "id")
      public Long id;
    
      @Column(name="contact_no")
      public String contactNo;
    
      @Column(name="country_code")
      public int countryCode;
    
      @Column(name="address_id")
      public int addressId;
    
      ....
    }