1

I try similar questions but without success. Please, Can someone help me on the question. I have the following POJOs:

@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 ......

And I try to retrieve a list of objects of EntityA that are not present in the field list contained in EntityB

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 );

and caught the exception

org.hibernate.QueryException: illegal attempt to dereference collection

Already tried also without subquery, passing as parameter the list of objects EntityA contained in the field of entityB, but again without success.

Can anyone tell me where I am wrong

Thanks in advance

1 Answer 1

1

Try this query:

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";
    ")";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Nicolae Albu. It worked just right. I do not have much knowledge in this JOIN clause. I'll look for more information. Again, thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.