java hibernate标准
我的数据库中有以下表结构:java hibernate标准,java,hibernate,criteria,Java,Hibernate,Criteria,我的数据库中有以下表结构: Table A: Table B: Table C: _______________________ ______________________________ ______________________ | Field | Type | Key | | Field | Type | Key | | Field | Type | Ke
Table A: Table B: Table C:
_______________________ ______________________________ ______________________
| Field | Type | Key | | Field | Type | Key | | Field | Type | Key |
______________________| |_____________________________| |_____________________|
| a_id | Int | PRI | | a_id | Int | FK of A(a_id)| | c_id | Int | PRI |
| ... ... | | c_id | Int | FK of C(c_id)| | ... |
|_____________________| ______________________________ |_____________________|
目标是:
class A {
List<C> list;
...
}
下面是一个SQL查询,它为我提供了正确的结果,现在应该在一个条件中进行“转换”:
select c.*
from A as a,
B as b,
C as c
where a.a_id = b.a_id and b.c_id = c.c_id;
有人知道如何制定标准吗?如果有
class A {
List<C> list;
...
}
此时,如果您执行criteria.list()
操作,您将获得所有a
对象,每个对象都有一个C
值列表,其中只有ID。为了获得完整的C对象,您需要额外的:
criteria.createAlias("list", "listC");
这将为A
和C
创建一个别名(即内部连接)。现在,假设您有一个a(属于a
)类,您可以执行a.getList().get(i).getWhateverFieldFromC()
;现在,您已经为A
的每个实例填充了C
s的列表
不要忘记在最后执行criteria.list()
检索列表。就这些了
p.S.这是假设(当然)实体
A
和C
正确映射。您尝试过使用HQL而不是标准api吗?您的实体看起来如何?所有其他数据库访问都是使用标准完成的,因此我希望坚持使用该标准;我添加了有效的SQL查询,并且应该在条件中“翻译”该查询
// Create criteria on class A
Criteria criteria = session.createCriteria(A.class);
criteria.createAlias("list", "listC");