如何在Hibernate中注释具有相同类型实体集的实体

时间:2014-02-13 14:58:22

标签: java hibernate annotations

我有2张桌子。第二个表包含两个FK到第一个(意思是用户可以拥有其他用户的团队)。

CREATE TABLE "user" (
                usr_id INTEGER PRIMARY KEY,
                usr_login VARCHAR NOT NULL UNIQUE,
);

CREATE TABLE user_team (
        utm_id INTEGER PRIMARY KEY,
        usr_id INTEGER NOT NULL UNIQUE,
        manager_id INTEGER NOT NULL,
        CONSTRAINT utm_usr_fk FOREIGN KEY (usr_id)
                REFERENCES "user" (usr_id) ON DELETE CASCADE,
        CONSTRAINT utm_manager_fk FOREIGN KEY (manager_id)
                REFERENCES "user" (usr_id)  ON DELETE CASCADE
);

我可以在User类中编写一些注释映射来拥有一组用户(团队)吗? 例如:

@Entity
@Table(name = "user")
public class User implements Serializable {
    @OneToMany ??????????????????????
    private List<User> team;
}

1 个答案:

答案 0 :(得分:1)

@JoinTable(name = "user_team", joinColumns = { @JoinColumn(name = "usr_id") }, inverseJoinColumns = { @JoinColumn(name = "manager_id") })

user_team没有utm_id列。这很重要吗?