Hibernate映射来自同一表的列值的集合

时间:2019-02-28 01:10:58

标签: java hibernate

给出下表:

CREATE TABLE MY_SCHEMA.TABLE_A (
    id integer NOT NULL,
    value character varying(50) NOT NULL,
    linked_id integer
)

我想知道休眠注释以完成我的实体类:

public class TableA {
    @Id @Column(name="id", unique=true)
    private int id;
    @Column(name="value", nullable=false)
    private String value;
    // missing annotations
    private String linkedValue;
}

这样我就可以在实体类{中将linkedValue(来自TABLE_A上的linked_id = id的自联接,并从列value中选择)作为字符串。 {1}}。

同样,如果不是一对一的话:

TableA

完成我的实体类的休眠注释是什么

CREATE TABLE MY_SCHEMA.TABLE_A (
    id integer NOT NULL,
    value character varying(50) NOT NULL
)

CREATE TABLE MY_SCHEMA.TABLE_B (
    table_a_id integer NOT NULL,
    linked_table_a_id integer NOT NULL
)

这样我就得到了public class TableA { @Id @Column(name="id", unique=true) private int id; @Column(name="value", nullable=false) private String value; // missing annotations private Set<String> linkedValues; } 的集合(这来自于linkedValues上的TABLE_ATABLE_B的连接以及{{1}上的id = table_a_id的重新连接。 },从实体类TABLE_A的{​​{1}}列中进行选择。

无需引入额外的实体类linked_table_a_id = id,即可选择答案。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用JPA 2.0的 Identity added: /tmp/git-resource-private-key (me@my-workstation) To <redacted>.git ! [rejected] HEAD -> develop (non-fast-forward) error: failed to push some refs to 'git@<redacted>.git' hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpart. Check out this branch and integrate the remote changes hint: (e.g. 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

@ElementCollection