场景:基于 xml 映射的 Hibernate 3.6、Java7、Postgresql 8.3。
我目前正在重构一个应用程序,其中我有这个数据库场景:
main_table
id integer
other_field string
(id) PK
secondary_table
other_field string
value string
(other_field, value) PK
基本上,有一个辅助表,其中包含与主表匹配的“other_field”;我需要提取 main_table 中特定记录的所有值并映射它们。
在 SQL 中,我会使用如下查询:
SELECT value FROM secondary table INNER JOIN main_table ON secondary_table.other_field == main_table.other_field where main_table.id = 1;
但我不明白如何使用这样的查询(或类似的查询,如果我提出的查询对 hibernate 不友好的话)将一组基本类型(字符串)映射到 Java 中的 Main 对象,这样我就可以在我的映射对象上有一个“值”属性,它应该是一个 Set
最佳答案
我想这就是您要找的:
@Entity
public class Primary { // Main table
@Id
@Column(name="EMP_ID")
private long id;
...
@ElementCollection
@CollectionTable(
name="PRIMARY_SECONDARY",
joinColumns=@JoinColumn(name="PRIMARY_ID")
)
private Set<Secondary> phones;
...
}
@Embeddable
public class Secondary { // Secondary table
private String value;
...
}
关于java - Hibernate:使用备用连接列映射集合属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23741986/