java 休眠 - 如何加入一个 EmbeddedId 列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26626920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hibernate - how to JoinColumn an EmbeddedId
提问by villager
Situation: I have a masterDB and a library1DB and library2DB.
情况:我有一个 masterDB 和一个 library1DB 和 library2DB。
library1DB and library2DB are two separate database but has the same schema because each library must have their own database. Now we have a masterDB, this is an aggregated version of all the data in all libraries (library1DB and library2DB), still taking note of their respective ids and mapping them to their library id.
library1DB 和 library2DB 是两个独立的数据库,但具有相同的架构,因为每个库都必须有自己的数据库。现在我们有了一个 masterDB,这是所有库(library1DB 和 library2DB)中所有数据的聚合版本,仍然记下它们各自的 id 并将它们映射到它们的库 id。
Here's I want my tables to be structured: book - book_id - library_id - title - shelf_id shelf - shelf_id - library_id - book_id - description
这是我希望我的表格结构化:书 - book_id - library_id - 标题 -shelf_id 书架 -shelf_id - library_id - book_id - 描述
I have these models:
我有这些模型:
@Entity
public class Book {
@EmbeddedId
private BookKey bookKey;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "SHELF_ID", referencedColumnName = "SHELF_ID"),
@JoinColumn(name = "LIBRARY_ID", referencedColumnName = "LIBRARY_ID")
})
private ObjectA objectA;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "ANOTHER_ID", referencedColumnName = "ANOTHER_ID"),
@JoinColumn(name = "LIBRARY_ID", referencedColumnName = "LIBRARY_ID")
})
private ObjectB objectB;
@Column
private String title;
}
@Embeddable
public class BookKey implements Serializable {
@Column(name = "BOOK_ID")
private long bookId;
@Column(name = "LIBRARY_ID")
private long libraryId;
}
But I get this exception:
但我得到这个例外:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: Book column: library_id (should be mapped with insert="false" update="false")
I've tried using @IdClass instead of @Embeddable and @EmbeddedId, and I got this:
我试过使用@IdClass 而不是@Embeddable 和@EmbeddedId,我得到了这个:
Caused by: org.hibernate.DuplicateMappingException: Table [book] contains physical column name [libraryId] represented by different logical column names: [libraryId], [LIBRARY_ID]
Any help?
有什么帮助吗?
Thanks!
谢谢!
回答by Alex
You should add insert="false", update="false"
for the second mapped column library_id
.
Try this:
您应该insert="false", update="false"
为第二个映射列添加library_id
。试试这个:
@JoinColumn(name = "LIBRARY_ID", referencedColumnName = "LIBRARY_ID",
insertable = false, updatable = false)
回答by Ben
I was getting the same problem. If you add insert="false", update="false" only to one, you will get an exception stating that you mixed writable and non-writables and that this is not allowed. The following works:
我遇到了同样的问题。如果仅将 insert="false", update="false" 添加到一个,您将收到一个异常,指出您混合了可写和不可写,这是不允许的。以下工作:
I solved it using the @PrimaryKeyJoinColumns, try this (from another example):
我使用@PrimaryKeyJoinColumns解决了它,试试这个(从另一个例子):
@ManyToOne
@PrimaryKeyJoinColumns(value = {
@PrimaryKeyJoinColumn(name = "country_code", referencedColumnName = "country_code"),
@PrimaryKeyJoinColumn(name = "zip_code", referencedColumnName = "code")
})
private Zip zip;
@ManyToOne
@PrimaryKeyJoinColumns(value = {
@PrimaryKeyJoinColumn(name = "country_code", referencedColumnName = "country_code"),
@PrimaryKeyJoinColumn(name = "state_code", referencedColumnName = "state_code"),
@PrimaryKeyJoinColumn(name = "city_name", referencedColumnName = "name")
})
private City city;
from Hibernate throws AnnotationException on column used by multiple overlapping foreign keys