org.hibernate.QueryException:非法尝试取消引用集合 - SubQuery错误

时间:2011-07-18 16:51:02

标签: hibernate jpa

我尝试类似的问题,但没有成功。拜托,有人可以帮我解决这个问题。我有以下POJO:

@Entity
public class EntityA implements Serializable  {

private static final long serialVersionUID = 1L;


@Id
@GeneratedValue(strategy = GenerationType.AUTO) 
private int id;

@Column(length=9)
private String someField;

@Column(length=50)
private String anotherField;

Getters and Setters ......




    @Entity
    public class EntityB {


private static final long serialVersionUID = 1L;


@Id
@GeneratedValue(strategy = GenerationType.AUTO) 
private int id;


@OneToMany(cascade={CascadeType.ALL} )
private List<EntityA> listOfEntityA;


Getters and Setters ......

我尝试检索EntityA中包含的字段列表中不存在的EntityA对象列表

return (List<EntityA>) genericDAO.retrieveList("from EntityA e1 where e1.id not in ( select e2.listOfEntityA.id from EntityB e2 where e2.id =?1)" , myParamId );

并抓住了异常

org.hibernate.QueryException: illegal attempt to dereference collection

在没有子查询的情况下也已尝试过,将entityB中包含的对象EntityA列表作为参数传递,但再次没有成功。

谁能告诉我哪里错了

提前致谢

1 个答案:

答案 0 :(得分:1)

尝试此查询:

String query =
    "SELECT entityA " +
    "FROM EntityA entityA " +
    "WHERE entityA.id NOT IN (" +
    "   SELECT entityAOfB.id " +
    "   FROM EntityB entityB " +
    "   JOIN entityB.listOfEntityA entityAOfB " +
    "   WHERE entityB.id = ?1";
    ")";