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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:50:09  来源:igfitidea点击:

JPA Annotations - How to retrieve a single value from a different table than the current object?

javahibernatejpaannotationshibernate-annotations

提问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_CODEfield 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;
}