当我使用hibernate查询两个表时。
SQLQuery query = session.createSQLQuery("select {a.*},{b.*} from a,b where
a.id=b.id");
query.addEntity("a",A.class);
query.addEntity("b",B.class);
List list = query.list();
列表可以转换为json,因为
[
[{id:1,aa:"aa"},{id:1,"bb":"bb"}],
[{id:2,aa:"aa2"},{id:2,"bb":"bb2"}]
可以在hibernate查询列表中获取如下所示的结果
[
{
a:{id:1,aa:"aa"},
b:{id:1,"bb":"bb"}
},
{
a:{id:2,aa:"aa2"},
b:{id:2,"bb":"bb2"}
}
]
答案 0 :(得分:0)
你应该将结果集包装成一些复杂的POJO和
public class ABListPojo{
private List<ABPojo> abList;
// ...getters and setters
}
public class ABPojo{
private A a;
private B b;
// ...getters and setters
}
并将setResultTransformer添加到查询
中query.setResultTransformer(Transformers.aliasToBean(ABListPojo.class))