休眠:@SecondaryTable不起作用 [英] Hibernate : @SecondaryTable doesn't work

查看:96
本文介绍了休眠:@SecondaryTable不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,@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.

我的数据库中有两个表(firstTablesecondTable),两个POJO Hibernate类(FirstTablePojoSecondTablePojo).

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中的featureNamefirstTable中的字段一起显示. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆