java Hibernate:从另一个表列映射值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16065379/
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
Hibernate: mapping a value from another table column
提问by user1409920
I have a domain entity looks like this:
我有一个域实体,如下所示:
class Exch{
private int id;
private String val1;
private String val2;
@Column(name = "val1")
public String getVal1(){
return val1;
}
@Column(name = "val2")
public String getVal2(){
return val2;
}
@Column(name = "id")
public int id(){
return id;
}
}
and table:
和表:
exch
交易所
Exch_ID| VAL1 | VAL2
Exch_ID| VAL1 | VAL2
100000 | AAA | BBB
100000 | AAA | BBB
200000 | CCC | DDD
200000 | CCC | 直拨电话
now I have another table which looks like:
现在我有另一个表,它看起来像:
exch_extra
exch_extra
Exch_ID| VAL3 | VAL4
Exch_ID| VAL3 | VAL4
100000 | ZZZ| YYY
100000 | ZZZ| 年年
200000 | XXX| UUU
200000 | XXX| 乌乌
is there anyway I can map val3 in exch_extra table to Entity Exch without creating an extra Exch_extra entity?
无论如何我可以将 exch_extra 表中的 val3 映射到 Entity Exch 而不创建额外的 Exch_extra 实体?
so i can have:
所以我可以:
class Exch{
private int id;
private String val1;
private String val2;
private String val3;
@Column(name = "val1")
public String getVal1(){
return val1;
}
@Column(name = "val2")
public String getVal2(){
return val2;
}
@ do something here so i can have val3 from exch_extra table
public String val3(){
return val3
}
@Column(name = "id")
public int id(){
return id;
}
}
回答by Amir Pashazadeh
Hibernate has an @SecondaryTable
annotation, which helps you map multiple table to an entity.
Hibernate 有一个@SecondaryTable
注释,它可以帮助您将多个表映射到一个实体。
Just add secondary table annotation, join column (in secondary table) annotation.
只需添加辅助表注释,连接列(在辅助表中)注释。
Then map other columns like:
然后映射其他列,如:
@Column(name="val3", table="table2")
private int val3;
where table2
is name of the other table.
table2
另一个表的名称在哪里。