JPA注释-如何从与当前对象不同的表中检索单个值? [英] JPA Annotations - How to retrieve a single value from a different table than the current object?
本文介绍了JPA注释-如何从与当前对象不同的表中检索单个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将一个值从另一个表的列映射到当前对象?
How do you map a single value from a column in another table to the current object?
示例:
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;
}
表格:Fields
Foo:FOO_ID, FOO_A
栏:FOO_ID, BAR_CODE
如何在不使用JPA annotations
创建单独的对象(或辅助表)的情况下检索BAR_CODE
字段?
How do I retrieve the BAR_CODE
field without creating a separate object (or a secondary table) using JPA annotations
?
推荐答案
使用第二表格.这样,您可以一对一地为一个实体映射另一个表,并定义使用该表的列映射.
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.
示例:
@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;
}
这篇关于JPA注释-如何从与当前对象不同的表中检索单个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文