Hibernate request with an alias

what happened with

session.createCriteria(Composed.class, "main") .createAlias("main.id.branch", "b1") .add(Restrictions.eq("b1.owner", user)) .list(); 

? Corresponding HQL works fine

 String hql = "select main from Composed as main" + "left join main.id.branch as b1 where b1.owner = ?"; session.createQuery(hql) .setInteger(0, user.id().intValue()) .list(); 

Of the criteria, Hibernate does not create a connection and uses where b1x1_.owner_id=? but there is no b1x1_ anywhere, so it fails with "cannot prepare statement".

The classes are pretty trivial

 @Entity class Composed { @Id ComposedId id; // also tried @EmbeddedId ... irrelevant stuff } @Embeddable class ComposedId { @ManyToOne(optional=false) Branch branch; ... irrelevant stuff } @Entity class Branch { @Id Integer id; @ManyToOne(optional=false) User owner; ... irrelevant stuff } @Entity class User { @Id Integer id; ... irrelevant stuff } 

Update

Finally, I created SSCCE and sent issue . Sorry for the confusing question, without SSCCE, it's pretty hard to reproduce.

+5
source share
2 answers

I have not tried, but maybe creating two aliases with a left join will help you. I mean this:

 session.createCriteria(Composed.class, "main") .createAlias("main.id", "id1", JoinType.LEFT_OUTER_JOIN) .createAlias("id1.branch", "b1", JoinType.LEFT_OUTER_JOIN) .add(Restrictions.eq("b1.owner", user)) 

Hope this helps!

+1
source

You need subcategories to connect instead of an alias, because Branch is a displayable Entity .

 session.createCriteria(Composed.class, "main") .createCriteria("main.id.branch", "b1") .add(Restrictions.eq("b1.owner", user)) .list(); 
0
source

Source: https://habr.com/ru/post/1237457/


All Articles