在PLAY 2.0中无法使用外键创建复合主键 [英] Can't create composite primary key with foreign key in PLAY 2.0

查看:98
本文介绍了在PLAY 2.0中无法使用外键创建复合主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想在我的PLAY项目中表示的情况:

This is a situation I want to represent in my PLAY project:

table clients {
     client_id (pk),
     description
}

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

在'items'表中,我想要一个由组合的client_id和item_id组成的复合主键。我已经阅读了JPA文档以及关于该主题的许多帖子,但一切都一次又一次失败。这是我尝试的许多版本之一 - 最接近JPA文档。

In the 'items' table I want to have a composite primary key that will consist of combined client_id and item_id. I have read JPA documentation as well as many posts on the topic but everything fails again and again. This is one of many versions I have 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) {...
}

以上代码(以及许多其他不同的设置)在播放启动期间产生以下错误:

The above code (as well as many other different setups) produce following error during play launch:


java.lang.RuntimeException:在com.avaje.ebeaninternal.server.deploy.parse读取
models.ItemsPK
的注释时出错。 ReadAnnotations.readAssociations(ReadAnnotations.java:73)
~ [ebean.jar:na]
at com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.readDeployAssociations(BeanDescriptorManager.java:1100)
~ [ebean.jar:na]

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

我不知道我的代码可能有什么问题。我开始认为这是一个播放错误。有什么想法吗?

I don't have a clue what might be wrong with my code. I'm starting to think that it is a PLAY bug. Any ideas?

推荐答案

Cześć,

这不是答案,相当建议。你能告诉我在你的情况下使用复合PK的主要目标是什么?使用Ebean ORM,你的两个模型都很小而且很容易

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

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

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;
}

这就是全部。除了复合PK

and that's all. It gives you what you need except the composite PK

这篇关于在PLAY 2.0中无法使用外键创建复合主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆