4

我不知道如何使用带有注释和休眠的复合键......

因此,例如,我有这张表:

表_A

初始化,数字,CNT,典型值

表_B

初始化、数字、V_CNT

TABLE 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;
}

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

4

1 回答 1

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;
}
于 2012-05-25T19:37:15.060 回答