java 是否可以在不定义任何关联的情况下映射实体中的字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4518266/
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
Is it possible to map a field in an Entity without defining any association?
提问by mdomenjoud
I've got the following schema in DB (simplified)
我在数据库中有以下架构(简化)
MainTable(
ID primary key
SOMEFIELD
CODE_FK1 -- references OtherTable1 CODE (without declared foreign key)
CODE_FK2 -- references OtherTable2 CODE (without declared foreign key)
... Other fields used
)
OtherTable1(
CODE primary key
LABEL
... other fields not used
)
OtherTable2(
CODE primary key
LABEL
... other fields not used
)
I'm asking if there is any way to define my Entity for main table in order to use directly labels from my other tables, i.e without defining entities for these other table.
我在问是否有任何方法可以为主表定义我的实体,以便直接使用我的其他表中的标签,即不为这些其他表定义实体。
I cannot change the DB schema, which is really awful (there are labels/code couples everywhere, defined in multiples tables). And If it was possible, this solution would allow to keep my code simple, since I don't really need these other entities.
我无法更改数据库架构,这真的很糟糕(到处都有标签/代码对,在多个表中定义)。如果可能的话,这个解决方案可以让我的代码保持简单,因为我真的不需要这些其他实体。
I guess it would result something like that:
我想它会导致这样的结果:
@Entity
public class MainEntity{
@Id
private Integer ID;
@Column(name="SOMEFIELD")
private String SomeField;
@SomeAnnotation to Join CODE_FK_1 with OtherTable1.CODE
@SomeAnnotation like @Column(name="LABEL", table="OtherTable1")
private String Label1;
@SomeAnnotation to Join CODE_FK_1 with OtherTable1.CODE
@SomeAnnotation like @Column(name="LABEL", table="OtherTable1")
private String Label1;
}
Thanks by advance for your help!
预先感谢您的帮助!
回答by Mopper
Another possibility would be using the @Formula
annotation to fetch the value from the other table. This will automatically generate a subselect whenever you load your Entity.
另一种可能性是使用@Formula
注释从另一个表中获取值。每当您加载实体时,这将自动生成一个子选择。
I think you'll need something like this:
我想你需要这样的东西:
@Entity
public class MainEntity{
@Id
private Integer ID;
@Column(name="SOMEFIELD")
private String SomeField;
@Formula("(SELECT ot1.LABEL FROM OtherTable1 ot1 WHERE ot1.CODE = CODE_FK_1)")
private String Label1;
}
There is little information about this in the [Hibernate docs][1], so you may need some trial and error to get it right (but you should be able to work it out with hibernate.show_sql=true
.
[Hibernate docs][1] 中关于此的信息很少,因此您可能需要进行一些试验和错误才能使其正确(但您应该能够使用hibernate.show_sql=true
.
There are 2 possible downsides to this approach:
这种方法有两个可能的缺点:
- This is hibernate-specific code
- This is plain SQL, and may thus be database-specific
- 这是休眠特定的代码
- 这是普通的 SQL,因此可能是特定于数据库的
HTH
HTH
[1]: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-propertyhibernate docs
[1]:http: //docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-property休眠文档
回答by jpkrohling
You can use the @SecondaryTable annotation. See this example:
您可以使用@SecondaryTable 注释。看这个例子: