JPA 注释 - 如何从与当前对象不同的表中检索单个值? [英] JPA Annotations - How to retrieve a single value from a different table than the current object?

查看:13
本文介绍了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;
}

表格:字段

Foo: FOO_ID, FOO_A

条形:FOO_ID、BAR_CODE

如何使用 JPA 注释 在不创建单独对象(或辅助表)的情况下检索 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆