Hibernate exception PropertyNotFoundException when using Transformer
hibernate, java
Solution
Problem found:
Even if the HQL can be translated to sql correctly, but when ResultTransformer gets the result, there will be only 3 fields in the result:
1. A
2. length
3. unit
No matter how many fields in A, they will be aggregated into a single field "A" and since I didn't set any alias to this field, it will be treated as "field 0".
So the problem solved after I changed the HQL like this:
select a.id as id, a.name as name, sum(c.length) as length, min(c.unit) as unit
from A a
left outer join a.b as b
left outer join b.c as c
group by
a.id
a.name
Problem
I'm using hibernate and hql for querying in my Java codes. But I got an exception like this: ``` Caused by: org.hibernate.PropertyNotFoundException: Could not find setter for 0 on class [my class] at org.hibernate.property.ChainedPropertyAccessor.getSetter(ChainedPropertyAccessor.java:44) ``` I don't understand what the "0" means. Here are some deatils with examples: I have several tables joining hql. The tables are like this: ``` A - A_ID - NAME B - B_ID - A_ID C - C_ID - B_ID - LENGTH - UNIT ``` Classes: ``` @Entity @Table(name="A") class A { @Id @Column(name="A_ID", updatable=false) private Long id; @Column(name="NAME", nullable=false, length=10, updatable=false) private String name; @OneToMany(mappedBy="a", fetch=FetchType.LAZY, cascade={CascadeType.ALL}) @JoinColumn(name="A_ID", nullable=false) private Set<B> bs; @Transient private Double length; @Transient private String unit; // Setters and getters ... } @Entity @Table(name="B") class B { @Id @Column(name="B_ID", updatable=false) private Long id; @ManyToOne @JoinColumn(name="A_ID", nullable=false, insertable=true, updatable=false) private A a; @OneToMany(mappedBy="b", fetch=FetchType.LAZY, cascade={CascadeType.ALL}) @JoinColumn(name="B_ID", nullable=false) private Set<C> cs; // Setters and getters ... } @Entity @Table(name="C") class C { @Id @Column(name="C_ID", updatable=false) private Long id; @ManyToOne @JoinColumn(name="B_ID", nullable=false, insertable=true, updatable=false) private B b; @Column(name="LENGTH", nullable=false, updatable=false) private Double length; @Column(name="UNIT", nullable=false, length=10, updatable=false) private String unit; // Setters and getters ... } ``` hql: ``` select a, sum(c.length) as length, min(c.unit) as unit from A a left outer join a.b as b left outer join b.c as c group by a.id a.name ``` Query: ``` Query query = session.createQuery(hql.toString()).setResultTransformer(Transformers.aliasToBean(A.class)); ``` The result is a list of object "A" with the length and unit collected. I don't understand why I got this exception. Please give some advices. Update: I wrote a ResultTransformer and output all the "alias" to see the problem: ``` -> 0 -> length -> unit ``` It seems it treats the "A" besides with length and unit. There should be some problems with my HQL?