使用Transformer时出现Hibernate异常PropertyNotFoundException [英] Hibernate exception PropertyNotFoundException when using Transformer

查看:102
本文介绍了使用Transformer时出现Hibernate异常PropertyNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用hibernate和hql来查询我的Java代码。但是我得到了这样的异常:

 原因:org.hibernate.PropertyNotFoundException:无法在类[我的班级] 
at org.hibernate.property.ChainedPropertyAccessor.getSetter(ChainedPropertyAccessor.java:44)

我不明白0是什么意思。这里有一些与例子deatils:

我有几个表加入hql。这些表如下所示:

  A 
- A_ID
- NAME

B
- B_ID
- A_ID

C
- C_ID
- B_ID
- 长度
- 单位

课程:

  @Entity 
@Table(name =A)
class A
{
@Id
@Column(name =A_ID,updatable = false )
私人长ID;

@Column(name =NAME,nullable = false,length = 10,updatable = false)
私有字符串名称;

@OneToMany(mappedBy =a,fetch = FetchType.LAZY,cascade = {CascadeType.ALL})
@JoinColumn(name =A_ID,nullable = false)
私人套餐< B> BS;

@Transient
私人双倍长度;

@Transient
私人字符串单位;

// Setter 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)
私人套餐< C> CS;

// Setter 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;

// Setter and getters
...
}

hql:

  select a,sum(c.length)as length,min(c.unit)as unit 
from a a
left外部连接ab as b
left外部连接bc as c
group by
a.id
a.name

查询:

  Query query = session.createQuery(hql.toString())。setResultTransformer(Transformers.aliasToBean(A.class)); 

结果是收集了长度和单位的对象A的列表。我不明白为什么我得到这个例外。请给出一些建议。






更新:



一个ResultTransformer并输出所有别名来查看问题:

   - > 0 
- >长度
- >单位

除了长度和单位之外,它似乎将A视为A。我的HQL应该会出现一些问题吗?解决方案



<即使HQL可以正确转换为sql,但是当ResultTransformer得到结果时,结果中只有3个字段:

  1。 A 
2.长度
3.单位

无论有多少字段A,它们将被聚合成单个字段A,并且由于我没有为该字段设置任何别名,因此它将被视为字段0。



所以问题解决后,我改变了这样的HQL:

 选择a.id作为id,a.name作为名称, sum(c.length)作为长度,min(c.unit)作为单位
从A a
离开外部连接ab作为b
离开外部连接bc作为c
group by
a.id
a.name


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?

解决方案

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

这篇关于使用Transformer时出现Hibernate异常PropertyNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆