【发布时间】:2012-05-23 15:43:51
【问题描述】:
这是我想在我的 PLAY 项目中表现的情况:
table clients {
client_id (pk),
description
}
table items {
client_id (fk, pk),
item_id (pk)
}
在“items”表中,我想要一个复合主键,它将由组合的 client_id 和 item_id 组成。我已阅读 JPA 文档以及有关该主题的许多帖子,但一切都一次又一次地失败。这是我尝试过的众多版本之一 - 最接近 JPA 文档。
@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) {...
}
上述代码(以及许多其他不同的设置)在播放启动期间产生以下错误:
java.lang.RuntimeException:读取注释时出错 模型.ItemsPK 在 com.avaje.ebeaninternal.server.deploy.parse.ReadAnnotations.readAssociations(ReadAnnotations.java:73) ~[ebean.jar:na] 在 com.avaje.ebeaninternal.server.deploy.BeanDescriptorManager.readDeployAssociations(BeanDescriptorManager.java:1100) ~[ebean.jar:na]
我不知道我的代码可能有什么问题。我开始认为这是一个 PLAY 错误。有什么想法吗?
【问题讨论】:
-
你有充分的理由坚持复合PK吗?即使您设法处理了这个问题,您也可能因此面临其他几个问题。为什么不向 items 表添加一个额外的列(比如 items_id)并在(client_id,item_id)上放置一个唯一索引。 (我想 item_id 指的是 item 表,就像 client_id 指的是客户表一样)。
-
@bpgergo 感谢您的回答。您的解决方案是可以接受的,但我宁愿避免额外的列和索引。令我惊讶的是,需要解决这样一个简单的案例。如果几天后没有解决方案,我会重新考虑。至于item_id,目前它没有引用任何其他表,但将来可能会改变。
-
标签:hibernate** 在这里不正确。没有 Hibernate 也没有 JPA
标签: java jpa playframework-2.0 ebean