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

前端 未结 1 954
情书的邮戳
情书的邮戳 2021-02-04 05:36

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         


        
相关标签:
1条回答
  • 2021-02-04 06:23

    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;
    }
    
    0 讨论(0)
提交回复
热议问题