Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 635
Tushar J Dudhatra Avatar asked Oct 15 '15 06:10

Tushar J Dudhatra


1 Answers

A class annotated @Embeddable must not contain any @EmbeddedId.

@EmbeddedId is to annotate a field of type @Embeddable class in another class and mark it as the primary key.

Example :

@Entity
public class Project {
    @EmbeddedId ProjectId id;
    ...
}

@Embeddable
Class ProjectId {
    int departmentId;
    long projectId;
    ...
}

EDIT : in your case, I don't think you want Phone, nor Address to be @Embeddable. Just mark them as regular @Entity.

like image 175
Gaël J Avatar answered Oct 03 '22 05:10

Gaël J