I have two tables with many-to-many relationships which called Position and Content.
Mapping is collect, because insert and and other queries all can work properly.
and I use
Criteria c = sessionFactory.getCurrentSession().createCriteria(Content.class);
c.add(Restrictions.isEmpty("position"));
to get the dataset which means all the Contents are not taken by any Position.
It works properly.
And then, I add:
Criteria c = sessionFactory.getCurrentSession().createCriteria(Content.class);
c.add(Restrictions.isEmpty("position"));
c.createAlias("position", "p");
c.add(Restrictions.eq("p.id", 100));
which means that to get all the Contents are not taken by Position 100.
I get a empty return.
And some suggested me using:
c.createAlias("position", "p");
c.add(Restrictions.not(Restrictions.eq("p.id", 100)))
It worked but with the result like this sql:
SELECT * FROM `position_has_content` WHERE content_id not in (select content_id from `position_has_content` where position_id = 100)
while I need the result like this:
SELECT * FROM `content` WHERE `content`.id not in (select content_id from `position_has_content` where position_id = 100)
Which is such different.
May I know what I missed here please?
I will keep updating when the problem solved.
The tables in the case like below:
position Position_has_content content
id position_id id
content_id
so, it is many-to-many case, they are already mapped properly in hibernate.
the phase of mapping relationships:
@ManyToMany(mappedBy = "content")
private Set<Position> position;
-------------------------------------------------------------
@ManyToMany(fetch = FetchType.EAGER,cascade=CascadeType.ALL)
@JoinTable(name = "position_has_content", joinColumns = {
@JoinColumn(name = "position_id") },
inverseJoinColumns = { @JoinColumn(name = "content_id") })
@OrderBy("position")
private Set<Content> content;
the mapping is correct, because all the other operations(normal EAGER query without "not in", insert, delete of both two sides) could work properly.