删除包含JPA和子查询的条目

时间:2014-03-14 15:58:01

标签: sql jpa criteria

我刚写了一个SQL查询:

DELETE FROM basisgegevens.gm_persoonburgstaat pbs 
WHERE (pbs.ingangsdatum, pbs.id_persoon) in (
  SELECT pbs2.ingangsdatum, pbs2.id_persoon 
  FROM basisgegevens.gm_persoonburgstaat pbs2 
  WHERE pbs2.ingangsdatum = pbs.ingangsdatum 
  AND pbs2.id_persoon = :persoonID 
  AND pbs2.id_persoonburgerlijkestaat > pbs.id_persoonburgerlijkestaat);

我需要将它重写为JPQL,但是我仍然坚持使用子查询来重新生成外部查询。

public class PersoonBurgerlijkeStaatEntity {
@Column(name = "id_persoonburgerlijkestaat"
private Long identifier;
private Date ingangsdatum;
@ManyToOne
@JoinColumn(name = "id_persoon", referencedColumnName = "id_persoon", nullable = false)
private PersoonEntity persoon;

}

persoon实体有一个标识符

有人可以帮我改写吗?

由于

1 个答案:

答案 0 :(得分:1)

不确定,但试一试。

DELETE FROM persoonburgstaat person where (person.ingangsdatum, person identifier) in 
    (select p.ingangsdatum, p.identifier from persoonburgstaat p
        left join p.persoon per where per.id_persoon = :persoonID 
        AND per.id_persoonburgerlijkestaa > p.identifier)

左连接将进行外部查询

但更确切地说,就像我想的那样,发布PersoonEntity实体" id_persoonburgerlijkestaa"是列的名称而不是属性,查询将基于此失败。