java JPA 注释 - 如何从与当前对象不同的表中检索单个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15503189/
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
JPA Annotations - How to retrieve a single value from a different table than the current object?
提问by Marcus
How do you map a single value from a column in another table to the current object?
如何将另一个表中某一列的单个值映射到当前对象?
Example:
例子:
class Foo {
@Id
@Column(name="FOO_ID")
private String fooId;
@Column(name="FOO_A")
private String fooA;
//Column to map to another table?
//is a one to one mapping - but don't want a separate object for this.
private String barCode;
}
Table: Fields
桌子: Fields
Foo: FOO_ID, FOO_A
福: FOO_ID, FOO_A
Bar: FOO_ID, BAR_CODE
酒吧: FOO_ID, BAR_CODE
How do I retrieve the BAR_CODE
field without creating a separate object (or a secondary table) using JPA annotations
?
如何在BAR_CODE
不使用创建单独对象(或辅助表)的情况下检索字段JPA annotations
?
回答by Perception
Use a secondary table. This allows you to map for an entity, on a one-to-one basis, another table and define column mappings that use it.
使用辅助表。这允许您在一对一的基础上为一个实体映射另一个表并定义使用它的列映射。
Example:
例子:
@Entity
@Table(name = "foo")
@SecondaryTable(name = "other_table", pkJoinColumns=@PrimaryKeyJoinColumn(name="id", referencedColumnName="FOO_ID"))
public class Foo {
@Id
@Column(name="FOO_ID")
private String fooId;
@Column(name="FOO_A")
private String fooA;
@Column(table="OtherTable", name="barCode")
private String barCode;
}