休眠:@SecondaryTable不起作用 [英] Hibernate : @SecondaryTable doesn't work
问题描述
我知道,@SecondaryTable
个问题被发布了无数次,因此,如果有相同的问题(我还没有找到),请给我链接或建议.
I know, @SecondaryTable
issues were published numerous times, so, if there is the same one (I haven't found it yet), please, give me the link or an advice.
我的数据库中有两个表(firstTable
和secondTable
),两个POJO Hibernate类(FirstTablePojo
和SecondTablePojo
).
I have two tables in my database (firstTable
and secondTable
), two POJO Hibernate classes (FirstTablePojo
and SecondTablePojo
).
+----------+ +-----------+
|firstTable| |secondTable|
|----------+ |-----------+
|featureId |------------>|featureId |(secondTable's primary key)
|foo | |featureName|
|bar | +-----------+
+----------+
我想在单个列表对象的jsp中显示这两个表中的字段,因此我决定使用@SecondaryTable
.这两个表通过featureId
字段(这是secondTable
的主键)连接在一起,我希望将secondTable
中的featureName
与firstTable
中的字段一起显示. FirstTablePojo
之前带有以下注释:
I want to show fields from both these tables in the jsp from the single list object, I decided to use @SecondaryTable
. These two tables are connected by the featureId
field (which is a primary key for the secondTable
), I want the featureName
from the secondTable
to be shown along with fields from the firstTable
. The FirstTablePojo
is preceded by this annotation:
@SecondaryTable(name="secondTable",
pkJoinColumns=@PrimaryKeyJoinColumn(name="featureId",
referencedColumnName = "featureId"))
我将此属性添加到了FirstTablePojo
(带有getter和setter):
I added this property to the FirstTablePojo
(with getters and setters):
@ManyToOne
@JoinColumn(name="featureId", table="secondTable")
String featureName;
在<c:forEach items="${features}" var="feature">
的帮助下,我得到了每个${feature.foo}
(foo
是在使用@SecondaryTable
之前在FirstTablePojo
中的属性)和${feature.featureName}
,我看到了每个foo
,但没有出现featureNames
.如果有人能告诉我在这里我想念什么,以及为什么其他表中的功能名称没有出现在FirstTablePojo
对象列表中,那将是非常不错的.
When with a help of <c:forEach items="${features}" var="feature">
, I get each ${feature.foo}
(foo
is a property that was in the FirstTablePojo
before I used @SecondaryTable
) and ${feature.featureName}
, I see each foo
, but none of the featureNames
appear. It'd be great if someone could tell me what do I miss here and why feature's names from the other table do not appear in the list of FirstTablePojo
objects.
推荐答案
@SecondaryTable
批注的重点是将单个实体的字段映射到多个表,就像将那些表合并到一个表中一样.
The point of the @SecondaryTable
annotation is to map the fields of a single entity to several tables, exactly as if those tables were merged into a single one.
@ManyToOne用于映射两个实体之间的多对一关联.但是你只有一个.在这种情况下,这没有任何意义. @JoinColumn用于指示字段映射到构成...连接列的列,即另一个表的外键.所以也没有道理.
@ManyToOne is used to map a many-to-one association betwen two entities. But you just have one. It makes no sense in this context. And @JoinColumn is used to indicate that a field is mapped to a column that constitutes a ... join column, i.e. a foreign key to another table. So it doesn't make sense either.
只需使用以下映射:
@Column(name="featureName", table="secondTable")
String featureName;
休眠文档.
这篇关于休眠:@SecondaryTable不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!