hibernate的hql語句怎麼寫
- 綜合知識
- 關注:1.66W次
不是有對映檔案嗎?比如你的資料庫表為student那麼的你就應該有hibernate.cfg.xml配置檔案和他的對映檔案hibernate.hbm.xml,最後還應該有一個student表的對映類student.java這3個檔案.然後你建一個實現類studentInsert實現是SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session session = sf.openSession(); Transaction tx = session.beginTransaction(); student st=new student(); st.setId("0001"); st.setUserName("Wang"); st.setpassWord("123"); session.save(st); tx.commit(); session.close();就是這樣。
2. 用hibernate的HQL怎麼寫一個插入語句不是有對映檔案嗎?
比如你的資料庫表為student
那麼的你就應該有hibernate.cfg.xml配置檔案和他的對映檔案hibernate.hbm.xml,最後還應該有一個student表的對映類student.java
這3個檔案.
然後你建一個實現類studentInsert
實現是
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
student st=new student();
st.setId("0001");
st.setUserName("Wang");
st.setpassWord("123");
session.save(st);
tx.commit();
session.close();
就是這樣.
3. 求寫一條hibernate的hql語句再套一層,select * from Tbobject where order_id in (。.你的一列資料)
HQL這樣寫
select * from TbOrder where t.id.orderId in (select distinct t.id.orderId from TbOrder t where t.tbViperson.viCardno=1)
-------------------------貌似不對奧----------
這樣,我以前SQL語句這樣寫過,
select distinct t.id.orderId ,max(列1),max(列2),max(列3) from TbOrder t where t.tbViperson.viCardno=1
4. 求寫一條hibernate的hql語句再套一層,select * from Tbobject where order_id in (。
.你的一列資料)HQL這樣寫select * from TbOrder where t.id.orderId in (select distinct t.id.orderId from TbOrder t where t.tbViperson.viCardno=1)-------------------------貌似不對奧----------這樣,我以前SQL語句這樣寫過,select distinct t.id.orderId ,max(列1),max(列2),max(列3) from TbOrder t where t.tbViperson.viCardno=1。
5. hibernate 查詢表中最後一條語句的hql怎麼寫select * from table
where rownum<(select count(*)+1 from table)
minus
select * from table
where rownum<(select count(*) from table)
也可以簡化為
select * from table
minus
select * from table
where rownum<(select count(*) from table)
效果是一樣的
切記rownum是偽列 只能用<
順便給你求第X行的通用SQL語句
select * from table where rownum<X+1
minus
select * from table where rownum<X
6. hibernate查詢表中最後一條語句的hql怎麼寫 select * from tablewhere rownum<(select count(*)+1 from table)minusselect * from tablewhere rownum<(select count(*) from table)也可以簡化為select * from tableminusselect * from tablewhere rownum<(select count(*) from table)效果是一樣的切記rownum是偽列 只能用<順便給你求第X行的通用SQL語句select * from table where rownum String hql="from 類名 where app=6 AND pro_read_flag=FALSE"; Query query=session.createQuery(hql); 或者 String hql="from 類名 where app=? AND pro_read_flag=?"; Query query=session.createQuery(hql); query.setParameter(0,6); query.setParameter(1,false); 首先要說的是,hibernate操縱的都是物件。所有第一條,你必須有一個javaBean,如是Cat, 有a和b屬性,有對應的get和set方法,通過new Cat('xyz',123);建立一個Cat物件cat,然後呼叫hibernate的session.save(cat);就ok了。第二條:首先要根據id把物件載入上來,然後更新,如:Cat cat = session.load(Cat.class,1),cat.setA('x');此時Cat類中必須有id和a屬性,有相應的get,set方法。第三:先把物件載入上來,然後刪除:Cat cat = session.load(Cat.class,'3'); session.delete(cat); 第四:就是根據Test物件的某個屬性查詢,返回的是Test物件的集合。 第五,沒發現dao和table有啥聯絡
- 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/zonghezhishi/xz9qz8.html