hibernate连接查询

时间:2023-12-20 15:13:26

Hibernate的HQL语言类似于SQL语言,更适合于Java面向对象的思想. 类与数据库映射好了,不必考虑数据库.

实现Class1的表与Class2的表的联合查询:

Class1的class2属性外键到Class2.

String hql = "SELECT new {package name}.{class@name}(*, *, *) from Class1 as c1, Class2 as c2 where c1.class2 = c2";  //将查询结果封装到新的类中.

List<class@name> list = null;  //定义要放的list.

Query query = session.create(hql);

list = query.list();

Reference:

[1] http://blog.sina.com.cn/s/blog_4f925fc30102dtsr.html