Search code examples
hibernatehibernate-criteria

Hibernate criteria one-to-many with embedded table


I've got tables A, B, C where B is association table with adding columns

| A | 1 .. * | B | * .. 1 | C |

Entity A with list of B entity

@Entity
@Table(name = "a")
public class A {
    //id

    @OneToMany(mappedBy="bPK.a", cascade={CascadeType.ALL})
    private List<B> listB; 

    // getters, setters
} 

Entity B has embedded PK

@Entity
@Table(name = "b")
public class B {
    @EmbeddedId
    private BPK bPK; 

    // added columns
    // getters, setters
}

Embedded primary key class:

@Embeddable
public class BPK implements Serializable {
    private static final long serialVersionUID = 1L;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="i_a", nullable=false)
    private A a;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="i_c", nullable=false)
    private C c;

    // getters, setters
}

Entity C

@Entity
@Table(name = "c")
public class C {
    @Column(name = "i_c")
    private Long iC;

    @Column(name="name")
    private String name; 

    // getters, setters
}

...and I would like to get such objects A which has associated object C with concrete name

I tried to do it this way but it doesnt work

Criteria criteria = session.createCriteria(A.class);  
criteria.createAlias("listB", "bList")
criteria.add(Restrictions.like("bList.bPK.c.name", "name", MatchMode.ANYWHERE));

I have got following exception:

org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: bPK.c.name

thank you for help


Solution

  • It seems this problem was reported as a bug.

    https://hibernate.atlassian.net/browse/HHH-6963