Hibernate createAlias ​​with proposal generates invalid request

I have the following tables: A: id B: id, text AB: aID, bID

I want joib A and B, where B.text contains the word "cat".

This is a sleep request:

Criteria c = session.createCriteria(TableA.class, "A");
c.createAlias("A.bs", "B", JoinType.INNER_JOIN, Restrictions.like("b.text", "%cat%"));
c.setProjection(Projections.property("id"));

Generated Request:

Select id 
FROM A a
INNER JOIN AB ab ON a.id=ab.aID AND (b.text like ?)
INNER JOIN B b ON b.id=ab.bID AND (b.text like ?)

For some reason AND (b.text like ?)appears in both internal compounds. As I understand it, it should only be in the second. This throws the following exception:

java.sql.SQLException: No value specified for parameter 2

I assume this is happening because it has only one parameter and two "?".

What am I missing?

EDIT:

Adding persistent classes:

@Entity
@Table(name="A")
Class A {

    @Id
    @Column(name="id", length=255)
    protected String id;

    @OneToMany
    @JoinTable(name="AB", joinColumns = @JoinColumn( name="aID"), inverseJoinColumns = @JoinColumn( name="bID"))
    @LazyCollection(LazyCollectionOption.FALSE)
    protected List<B> bs;

}

@Entity
@Table(name="B")
Class B {

    @Id
    @Column(name="id", length=255)
    protected String id;

    @Column(name="text", length=255)
    protected String text;

}
+4
source share
1 answer

I think you need to create an alias

c.createAlias("A.bs", "b", JoinType.INNER_JOIN, Restrictions.like("b.text", "%cat%"));

Update

, Hibernate. , where

 c.createAlias("A.bs", "b", JoinType.INNER_JOIN);
 c.add(Restrictions.like("b.text", "%cat%"));

B.

0

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


All Articles