【发布时间】:2016-11-01 15:15:21
【问题描述】:
我正在尝试将 oracle 结果列表绑定到 summary 列表。但是我的 summary 列表有 3 个类定义为 DB 的实体
我有三个实体类A、B、C
Summary.class
{
@Autowired
private A a;
@Autowired
private B b;
@Autowired
private C c;
//getters and setters
}
@Enity
class A{
Fields 1..n ;
} // same goes for other classes definition
我通过以下查询获得结果,但结果无法转换为摘要对象
List<Summary> summaryList = entityManager.createQuery("from A a, B b, C c" +
" where a.field1 = b.field1 and a.fValue = :fValue " +
"and b.field3= c.field3", Summary.class)
.setParameter("fValue ", fValue )
.getResultList();
调试: 我确保结果列表不为空,并且如果我不将其转换为对象,则下面的查询可以正常工作
List summaryList = entityManager.createQuery("from A a, B b, C c" +
" where a.field1 = b.field1 and a.fValue = :fValue " +
"and b.field3= c.field3")
.setParameter("fValue ", fValue )
.getResultList();
我看到的替代方法 1 是遍历 summaryList 并将它们分配给像这样的单个列表,我还没有测试过,但我认为它可能会给出一个类转换异常,因为之前的转换力起作用
for (int i = 0; i < summaryList.size(); i++) {
Summary s= (Summary) summaryList.get(i); // might be class cast Exception
aList.add(s.getA());
bList.add(s.getB());
}
我想的替代方案 2 是 只从 db 中获取 A 类字段列表,将其转换为 A 的列表,进行 3 次蛮力操作,直到我得到所有字段。
以下是我在创建新问题之前查看的一些问题
Uses a different class to combine multiple entity classes
gets a list back mapped to pojo
请让我知道您的想法,我认为我的主要方法是可行的好方法。
【问题讨论】:
标签: java oracle hibernate spring-boot hql