将MySQL选择转换为Criteria API

时间:2016-03-24 14:08:10

标签: java mysql hibernate jpa criteria-api

我在MySQL中有以下select语句(对于列/表名称很抱歉):

SELECT a1.* FROM A a1
LEFT JOIN A a2
ON a1.DD_ID = a2.DD_ID
and a1.PRD = a2.PRD
AND a1.VN < a2.VN
LEFT JOIN B bb
ON a1.id = cc.del_id
WHERE a2.DD_ID is null
AND bb.del_id is null;

以下是实体:

@Entity
@Table(name = "A", uniqueConstraints = {
    @UniqueConstraint(columnNames = {"DD_ID", "VN", "PRD"})})
public class A
    implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    private Long id;

    @Column(name = "VN", nullable = false)
    private Short vn;

    @Column(name = "PRD", nullable = false)
    @NotNull
    private String prd;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "DD_ID", nullable = false)
    @NotNull
    private DD dd;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "A")
    private Set<B> bs;

    @Override
    public boolean equals(final Object pOther)
    {
    ...
    }

    @Override
    public int hashCode()
    {
       ...
    }

    // getters and setters
}
@Entity
@Table(name = "B")
public class B
    implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "DEL_ID", nullable = false)
    @NotNull
    private A a;

    @Override
    public boolean equals(final Object pOther)
    {
        ...
    }
    // getters and setters
}

我想使用Criteria API实现它,但是我在语句中实现 self left join 时遇到了麻烦。

有什么建议吗?

0 个答案:

没有答案