Cannot create compound primary key with foreign key in PLAY 2.0

This is the situation I want to introduce in the PLAY project:

table clients { client_id (pk), description } table items { client_id (fk, pk), item_id (pk) } 

In the "items" table, I want to have a composite primary key that will consist of a combined client_id and item_id. I read the JPA documentation, as well as a lot of posts on this topic, but everything happens again and again. This is one of the many versions I've tried - closest to the JPA documentation.

 @Entity @Table(name = "items") public class Items extends Model { ItemsPK primaryKey; public Items() { } @EmbeddedId public ItemsPK getPrimaryKey() { return primaryKey; } public void setPrimaryKey(ItemsPK pk) { primaryKey = pk; } } @Embeddable public class ItemsPK implements Serializable { private long itemId; private Client client; public ItemsPK() { } @Column(name = "item_id") @GeneratedValue(strategy = GenerationType.AUTO) public long getItemId() { return itemId; } public void setItemId(long itemId) { this.itemId = itemId; } @ManyToOne @JoinColumn(name = "client_id", nullable = false) public Client getClient() { return client; } public void setClient(Client client) { this.client = client; } //public int hashCode() {... //public boolean equals(Object obj) {... } 

In the above code (as well as in many other settings), the following error occurs during launch launch:

java.lang.RuntimeException: error reading annotations for models.ItemsPK at com.avaje.ebeaninternal.server.deploy.parse.ReadAnnotations.readAssociations (ReadAnnotations.java:73) ~ [Ebean.jar: on] at com.avaje.ebeaninternal .server.deploy.BeanDescriptorManager.readDeployAssociations (BeanDescriptorManager.java:1100) ~ [Ebean.jar: on]

I have no clue what might be wrong with my code. I'm starting to think this is a PLAY error. Any ideas?

+4
source share
3 answers

CzΔ™Ε›Δ‡,

This is not an answer, rather a suggestion. Can you tell me what is the main purpose of using a composite PC in your case? Your both models would be very light and light with Ebean ORM

models / Client.java

 package models; import play.db.ebean.Model; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Client extends Model { @Id public Long id; public String description; } 

models / Item.java

 package models; import play.db.ebean.Model; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class Item extends Model { @Id public Long id; @ManyToOne public Client client; } 

and that’s it. It gives you what you need besides Compound PK

0
source

In my experience, you cannot use GeneratedValue inside EmbeddedId, values ​​in a compound key must be assigned. See Excerpt below.

Using the GeneratedValue annotation is only required to be supported for simple primary keys. Using the GeneratedValue annotation is not supported for derived primary keys.

http://docs.oracle.com/javaee/6/api/javax/persistence/GeneratedValue.html

http://www.objectdb.com/api/java/jpa/GeneratedValue

I would recommend not using a composite key for this, since itemId would be enough to create a unique identifier.

0
source

I think you have not disabled Ebean. Ebean is the default ORM for game 2. If you want to use JPA, you need to disable Ebean.

So, in your Build.scala add this:

 val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings( ebeanEnabled := false ) 

And in your application.conf edit this line:

 ebean.default="models.*" 
0
source

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


All Articles