选择使用JPA的非实体? [英] Select non-entities with JPA?

查看:67
本文介绍了选择使用JPA的非实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JPA是否可以通过本机查询检索非实体类的实例?
我有一个包装两个实体的非实体类:

Is it possible with JPA to retrieve a instances of a non-entity classes with native queries?
I have a non-entity class that wraps two entities:

class Wrap{
  Entity1 ent1;
  Entity2 ent2
}
@Entity
class Entity1{
  ...
}
@Entity
class Entity2{
  ...
}

我该怎么做?

Query q = entityManager.createNativeQuery("native select here");
List<Wrap> list = q.getResultList();

推荐答案

JPA是否可以通过本机查询检索非实体类的实例?

Is it possible with JPA to retrieve a instances of a non-entity classes with native queries?

不.本机查询只能返回实体(如果您通过将resultClassresultSetMapping传递给

No. Native queries can return entities only (if you tell them to do so by passing the resultClass or a resultSetMapping to the createNativeQuery method; if you don't, you will get collections of raw data).

在JPQL中,可以使用带有非实体构造函数的构造函数表达式(SELECT NEW ...).但是,本机查询不支持此功能,您必须手动执行此操作.

In JPQL, you can use constructor expressions (SELECT NEW...) whith a non-entity constructor. But this is not supported for native queries, you'll have to do it manually.

这篇关于选择使用JPA的非实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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