I am having two classes, say A
and B
.
A
has a set of B
as its property. B
has no idea about A
.
Class A {
@OneToMany(fetch=FetchType.LAZY)
@JoinTable(name = "A_B_MAPPING", joinColumns = @JoinColumn(name = "A_ID") , inverseJoinColumns = @JoinColumn(name = "B_ID"))
private Set<B> bs;
}
The problem is, when I want to delete those records of A
which do not have B
, HQL does not understand it! My code is like this:
String hql = "DELETE FROM A WHERE bs IS NULL";
Query query = getSession().createQuery(hql);
query.executeUpdate(); // <-- Here an exception is thrown
As it seems, the HQL is unable to understand that this property is not a column, and it has to join to the A_B_MAPPING
.
I have also noticed that the generated query is referring to this bs
property
as non-qualified-property-ref
!
So the question is, how can I modify my query to achieve my goal?
In HQL, you need to declare an alias to use it as your instance variable and access the fields. Try to modify your query like that:
String hql = "DELETE FROM A a WHERE a.bs IS NULL";
Another problem with HQL and collections is that maybe, the collection won't be null, but empty. In that case, you should use is empty
instead of is null
:
String hql = "DELETE FROM A a WHERE a.bs IS EMPTY";
I think you need the is empty
query form to solve your problem.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments