A strange problem when setting identifier types for the Embeddable class in EclipseLink-2.5.2

In my current implementation, I have separate entity classes for each db table. I use JPA along with eclipselink-2.5.2. This works fine for me, but at some point, when the data is huge, it lags behind. So I decided to start using @Embedded, @Embeddable and @EmbeddedId. By doing this, I get an error message that is very strange for me. Here is the full stacktrace post: https://gist.githubusercontent.com/tjDudhatra/b955812e0d1a71cf97f1/raw/11ea458869e24baae744530417ac99bc877ed514/gistfile1.txt

To be specific, let me give you the exact scenario, in which case I get an exception. Consider this block of code that has three classes. One of them is annotated as @Entity, and the other two are annotated as @Embeddable. I know that in one class we cannot define @Id and @EmbeddedId, and I didn’t do this, then when I deploy the server I get an exception that only says that:

[class org.apache. {SomeClass}] has both @EmbdeddedId (by attribute [id]) and @Id (by attribute []. Both identifiers cannot be specified on the same object.

@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 are needed and any help would be greatly appreciated.

Thanks,

+5
source share
2 answers

So here I found the problem. I did some analysis and found out more about embeddable and elementCollection. Here is the link: https://en.wikibooks.org/wiki/Java_Persistence/ElementCollection

So now I have changed the code and now it works fine. Here is how I did it:

 @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; .... } 
+3
source

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

@EmbeddedId - annotate a field of type @Embeddable class in another class and mark it as a 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 , and Address @Embeddable . Just mark them as regular @Entity .

+3
source

Source: https://habr.com/ru/post/1233759/


All Articles