从JPA Query.getResultList()映射到自定义的TO [英] mapping list from JPA Query.getResultList() to a customized TO

查看:1687
本文介绍了从JPA Query.getResultList()映射到自定义的TO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,里面有四列id,名称,颜色和形状。表中的

条目为:

  1,苹果,红色,圆形
2,香蕉,黄色,长形
3,番茄红色圆形
4,橙色,橙色,圆形

现在我创建了一个实体类Fruit映射到上表。

  @Entity 
@Table(name =fruit)
public class Fruit实现Serializable {

@Id
@Column(name =ID)
字符串ID;

@Column(name =NAME)
字符串名称;

@Column(name =COLOR)
字符串颜色;

@Column(name =SHAPE)
字符串形状;

// getters / setters去这里
}

我的DAO类的代码是:

  String myQuery =从f中选择f.shape,f.name f。 shape =:shape; 
查询查询= this.em.createQuery(myQuery);
query.setParameter(shape,round);

显而易见,运行上面的查询将返回3行。



我有一个简单的TO类FruitSearchTO

  class FruitSearchTO 
{
字符串形状;
字符串名称;

// getters / setters here
}

符合我查询返回的行。

但是在我的DAO中运行如下所示:

 列表与LT; FruitSearchTO> fruitList = new ArrayList< FruitSearchTO>(); 
fruitList = query.getResultList();

抛出异常java.lang.ClassCastException:[Ljava.lang.Object;与FruitSearchTO不兼容]



我在哪里错了,这个解决方案是什么?

解决方案

您使用的HQL将返回 List< Object []> code>是位置0中 shape 的数组,而位置1中是 name



您可以使用 List< FruitSearchTO> /hibernate/core/4.0/javadocs/org/hibernate/transform/Transformers.html#aliasToBean%28java.lang.Class%29rel =nofollow> AliasToBeanResultTransformer

  List fruitList = s.createQuery(
select f.shape as shape,f.name (f.shape =:shape;)
.setParameter(shape,paramShape)
.setResultTransformer(Transformers.aliasToBean(FruitSearchTO.class))
.list ();
FruitSearchTOdto =(FruitSearchTO)fruitList .get(0);

或者,如果 FruitSearchTO 具有适当的构造函数:,你也可以通过选择新的FruitSearchTO(f.shape,f.name)来实现。

查看关于HQL的Hibernate Reference章节,特别是 15.6 选择子句一章。


I have a table fruit with four columns id, name, color, shape.

entries in the table would be:

1, apple, red, round
2, banana, yellow, long
3, tomato, red, round
4, orange, orange, round

Now I made an entity class Fruit mapped to above table.

@Entity
@Table(name="fruit")
public class Fruit implements Serializable {

@Id
@Column(name="ID")
String id;

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

@Column(name="COLOR")
String color;

@Column(name="SHAPE")
String shape;

//getters/setters goes here
}

In my DAO class, the code is:

String myQuery = "Select f.shape, f.name from Fruit f where f.shape = :shape";
Query query = this.em.createQuery(myQuery);
query.setParameter("shape", "round");

As obvious, running above query will return 3 rows.

I have a simple TO class FruitSearchTO

class FruitSearchTO
{
  String shape;
  String name;

  //getters/setters here
}

This TO complies with the rows returned by my query.

But in my DAO running something like:

List<FruitSearchTO> fruitList = new ArrayList<FruitSearchTO>();  
fruitList = query.getResultList();

is throwing exception java.lang.ClassCastException: [Ljava.lang.Object; incompatible with FruitSearchTO]

Where am I going wrong and what is the solution to this ?

解决方案

The HQL you're using will return a List<Object[]>, each element of the List being an array with shape in position 0 and name in position 1.

You can make the HQL return a List<FruitSearchTO> using an AliasToBeanResultTransformer:

List fruitList = s.createQuery(
  "select f.shape as shape, f.name as name from Fruit f where f.shape = :shape;")
  .setParameter("shape", paramShape)
  .setResultTransformer( Transformers.aliasToBean(FruitSearchTO.class))
  .list();
FruitSearchTOdto = (FruitSearchTO) fruitList .get(0);

Alternatively, if FruitSearchTO has an appropriate constructor:, you can also achieve this with select new FruitSearchTO(f.shape, f.name).

Take a look at the Hibernate Reference chapter on HQL, particularly 15.6 The select clause chapter.

这篇关于从JPA Query.getResultList()映射到自定义的TO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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