情况:我有一个masterDB和一个library1DB和library2DB。
library1DB和library2DB是两个独立的数据库,但具有相同的模式,因为每个库必须拥有自己的数据库。现在我们有了一个masterDB,这是所有库(library1DB和library2DB)中所有数据的聚合版本,仍然记录它们各自的id并将它们映射到它们的库id。
这是我希望我的表格结构化: 书 - book_id - library_id - 标题 - shelf_id 架 - shelf_id - library_id - book_id - 说明
我有这些模特:
@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;
}
但我得到了这个例外:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: Book column: library_id (should be mapped with insert="false" update="false")
我尝试使用@IdClass而不是@Embeddable和@EmbeddedId,我得到了这个:
Caused by: org.hibernate.DuplicateMappingException: Table [book] contains physical column name [libraryId] represented by different logical column names: [libraryId], [LIBRARY_ID]
任何帮助?
谢谢!
答案 0 :(得分:7)
您应该为第二个映射列insert="false", update="false"
添加library_id
。
试试这个:
@JoinColumn(name = "LIBRARY_ID", referencedColumnName = "LIBRARY_ID",
insertable = false, updatable = false)
答案 1 :(得分:5)
我遇到了同样的问题。如果你添加insert =" false",update =" false"只有一个,你会得到一个例外,说明你混合了可写和不可写,并且这是不允许的。以下作品:
我使用 @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;
来自Hibernate throws AnnotationException on column used by multiple overlapping foreign keys