Hibernate連線查詢join
主表Owner 從表Tea
Owner及Tea的pojo
import java.util.HashSet;
import java.util.Set;
public class Owner {
private int id;
private String name;
private String Address;
private Set<Tea> set=new HashSet<Tea>();
....}
public class Tea {
private int id;
private int name;
private int sales;
private Owner owener;
......}
1.查詢物件:直接查詢返回一個符合條件的Object[][]的List
<pre class="java" name="code">
@Testpublic void test10(){
String hql=" from Owner o join o.set where owner_id = :newName";
Query query=session.createQuery(hql);
List<Object[]> list=query.setInteger("newName", 103).list();
tx.commit();
Iterator it=list.iterator();
while(it.hasNext()){
Object[] o=(Object[]) it.next();
System.out.print(o[0]);
System.out.println(o[1]);
}
}
結果:
Owner [id=103, name=李華_1, Address=合肥_1]Tea [id=205, name=2, sales=2]
Owner [id=103, name=李華_1, Address=合肥_1]Tea [id=206, name=3, sales=3]
2.查詢物件:在查詢物件前加上distinct 這時只返回一個符合條件的主表物件的List
@Test
public void test10(){
String hql=" select distinct o from Owner o join o.set where owner_id = :newName";
Query query=session.createQuery(hql);
List<Owner> list=query.setInteger("newName", 103).list();
tx.commit();
Iterator it=list.iterator();
while(it.hasNext()){
System.out.println(it.next().toString());
}
}
查詢結果:</pre></p><pre class="java" name="code">Owner [id=103, name=李華_1, Address=合肥_1]
3.查詢屬性:查詢單個屬性時返回一個Object查詢多個屬性時返回一個Object[][][][]....
distinct可加可不加
@Test
public void test1(){
String hql="select o.name from Owner o inner join o.set teas where teas.name= :a";
Query query=session.createQuery(hql);
query.setString("a","2");
// query.setInteger("b", 4);
List<String> olist=query.list();
tx.commit();
for(String s:olist){
System.out.println(s);
}
}
相關文章
- sql 連線查詢例項(left join)三表連線查詢SQL
- 連線查詢簡析 join 、 left join 、 right join
- Hibernate【查詢、連線池、逆向工程】
- MySql的join(連線)查詢 (三表 left join 寫法)MySql
- mysql常用連線查詢join,left,right,crossMySqlROS
- Hibernate-ORM:13.Hibernate中的連線查詢ORM
- mysql求交集:UNION ALL合併查詢,inner join內連線查詢,IN/EXISTS子查詢MySql
- MySQL join連表查詢示例MySql
- 連線查詢
- mysql INNER JOIN、LEFT JOIN、RIGHT JOIN;內連線(等值連線)、左連線、右連線MySql
- SQL連線查詢SQL
- oracle 連線查詢Oracle
- 【Oracle】--連線查詢Oracle
- Mybatis-Plus-Join(MPJ連表查詢)MyBatis
- Oracle(+)連線與Join連線Oracle
- MySQL之連線查詢和子查詢MySql
- mysql查詢語句5:連線查詢MySql
- select查詢之四:連線查詢
- MySQL之連線查詢MySql
- LEFT JOIN 和JOIN 多表連線
- MySQL筆記-左連線的使用(left join有關聯的多表查詢)MySql筆記
- Hibernate 查詢
- 資料庫 - 連線查詢、巢狀查詢、集合查詢資料庫巢狀
- 省去join的查詢
- join 查詢優化優化
- mysql-分組查詢-子查詢-連線查詢-組合查詢MySql
- oracle連線查詢詳解Oracle
- MySQL學習-連線查詢MySql
- 【janes】多表查詢 外連線
- MySQL8:連線查詢MySql
- 資料庫資料的查詢----連線查詢資料庫
- 水煮oracle31----連線查詢&合併查詢Oracle
- Hibernate查詢方式
- Hibernate——Query查詢
- oracle update left join查詢Oracle
- SQL中聯表查詢操作(LEFT JOIN, RIGHT JOIN, INNER JOIN)SQL
- MySQL內連線查詢語句MySql
- 分組查詢連線號段