2

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

Daniel
  • 256
  • 3
  • 18

3 Answers3

1

It seems this problem was reported as a bug.

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

Victor
  • 3,395
  • 1
  • 23
  • 26
Daniel
  • 256
  • 3
  • 18
0

The reason why you are getting that is that Hibernate is looking for column bPk.c.name in your bList (A class), and there is obviously no such column. What you are trying to achieve can be done my multiple aliases (when creating an alias, Criteria is also doing a join on your current table with that one).

Thus:

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

I think the MatchMode.ANYWHERE is not necessary.

Raul Rene
  • 10,014
  • 9
  • 53
  • 75
  • Thank you for the reply but it still doesnt work. But now I get another exception: org.springframework.dao.InvalidDataAccessResourceUsageException: Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'c3_.name' in 'where clause' – Daniel May 07 '12 at 13:17
  • org.springframework.dao.InvalidDataAccessResourceUsageException: Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'c3_.name' in 'where clause' – Daniel May 07 '12 at 13:20
  • Where is the `3` in `c3_.name` coming from? – Raul Rene May 07 '12 at 13:26
  • 1
    I think that hibernate index that aliases somehow. I had tried this way with aliases before I sent this post and it didnt work. When I throw away the embedded primary key from B entity and left the index property like a non-id column then it works fine. So I guess problem is somewhere in the embedded table. – Daniel May 07 '12 at 13:33
  • You are probably right. I am also curious to see a solution for this. I cannot help any further though, sorry. – Raul Rene May 07 '12 at 13:39
0

I tried to find a way to reference a collection element in a @CollectionTable with hibernate criteria for a whole day ! finally I found this :

ElementCollection createAlias in hibernate API

I needed to reference a basic element (Integer) in a collection, hope it works for non-basic elements as well. In case someone is lost like me , don't give up and turn to HQL, cause there is a solution!!! :)

Duf
  • 1