Can't create composite primary key with foreign key in PLAY 2.0

女生的网名这么多〃 提交于 2019-12-05 17:04:17

Cześć,

This not an answer, rather suggestion. Can you tell me what is main goal of using composite PK in your case? Your both models would be reaaaaly small and easy 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 all. It gives you what you need except the composite PK

In my experience, you are not able to use GeneratedValue inside an EmbeddedId, the values in a composite key must be assigned. See excerpt below.

The use of the GeneratedValue annotation is only required to be supported for simple primary keys. Use of 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 as itemId would be sufficient in generating a unique identifier.

I think that you haven't disabled Ebean. Ebean is the default ORM for Play 2. If you want to use JPA you have 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.*"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!