休眠和复合键

时间:2012-05-25 18:39:11

标签: hibernate annotations composite-primary-key

我迷失在如何使用带注释和休眠的复合键......

因此,例如我有这个表:

TABLE_A

INIT, NUM, CNT, TYP

表-B

INIT, NUM, V_CNT

表A和B主键是由INIT和NUM

组成的复合键

我想运行我的查询:

   "FROM TABLE_A AS A " +
   "LEFT JOIN A.hiber_join AS B " +
   "WHERE A.INIT||A.NUM IN (:carList) AND A.INIT IN (:initList) AND A.NUM IN              (:numberList) " + 
    "AND B.V_CNT > 0

我的课程看起来像:

*的 Class_A *

public class TABLE_A implements Serializable{

private String INIT;
private String NUM;
private Integer CNT;
private String TYP;

    @OneToOne
@JoinTable(name = "TABLE_B",
    joinColumns = {@JoinColumn(name = "INIT"), @JoinColumn(name = "NUM")}

)
protected TABLE_B hiber_join;

    public TABLE_A()
    {}

    public void doHibernateStuff()
    {
     //call query get result set
    }

}

// B类

public class TABLE_B implements Serializable{

private String NUM;
private String INIT;
private Integer V_CNT;
}

我试图让它变得尽可能简单,因为我需要从如何使用hibernate和注释的基础知识...你可以告诉我已经工作了一段时间和hibernate文档和其他问题没有似乎有帮助...

1 个答案:

答案 0 :(得分:1)

AFAIK,您需要使用PrimaryKeyJoinColumn(我更改了类和属性名称以符合标准Java命名约定,但您应该选择有意义的名称):

public class BEntity implements Serializable{

    @Id
    @Column(name = "NUM")
    private String num;

    @Id
    @Column(name = "INIT")
    private String init;

    @Column(name = "V_CNT")
    private Integer vcnt;
}

public class AEntity implements Serializable{

    @Id
    @Column(name = "NUM")
    private String num;

    @Id
    @Column(name = "INIT")
    private String init;

    @OneToOne
    @PrimaryKeyJoinColumns({
        @PrimaryKeyJoinColumn(name="NUM", referencedColumnName="NUM"),
        @PrimaryKeyJoinColumn(name="INIT", referencedColumnName="INIT")
    })
    private BEntity bEntity;
}