導航:首頁 > 源碼編譯 > hibernatehql編譯sql

hibernatehql編譯sql

發布時間:2022-01-19 05:17:35

⑴ Hibernate的HQL和sql有什麼區別

sql 面向資料庫表查詢

hql 面向對象查詢

hql : from 後面跟的 類名+類對象 where 後 用 對象的屬性做條件

sql: from 後面跟的是表名 where 後 用表中欄位做條件

查詢

在Hibernate中使用查詢時,一般使用Hql查詢語句。

HQL(Hibernate Query Language),即Hibernate的查詢語言跟SQL非常相像。不過HQL與SQL的最根本的區別,就是它是面向對象的。

⑵ hibernate的hql語句和sql語句一樣嗎

一樣.都是資料庫操作語句.
只是有區別.sql語句貌似裸奔.hql整裝.......
sql可比作用dos操作的老版windows系統.hql.....windows xp

⑶ 將普通的sql語句改寫成用Hibernate框架中的HQL語句。

我猜想你的目的是用hibernate框架來解決上面的查詢問題吧,那你不一定非要把sql改為hql語句,因為hibernate中是可以支持原生態的sql語句的,我來給你寫一個例子吧

java">Listlist=newArrayList();
Stringsql="SELECTb.*,MAX(l.endDate).bookID=l.bookIDWHEREb.bookNameLIKE'%語%'ANDb.authorLIKE'%王%'GROUPBYb.bookID;";
Sessionsession=this.getSession();
Queryquery=session.createSQLQuery(sql);
list=query.list();
session.close();

其中,session.createSQLQuery(sql)這句話指明了hibernate用的是原生態的sql語句

希望對你有所幫助。

⑷ Hibernate怎麼利用配置文件配置HQL/SQL

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<typedef class="org.jbpm.db.hibernate.StringMax" name="string_max" />

<!-- related to ProcessDefintion -->
<!-- ########################### -->
<!-- HQL -->
<query name="QueryUser">
<![CDATA[
FROM User u
WHERE u.username = :username
]]>
</query>
<!--SQL-->
<sql-query name="SqlQueryUser">
<![CDATA[
SELECT
{u.*}
FROM
user u
WHERE
u.user_name = :userName
]]>
<return alias="u" class="com.test.entity.User"/>
</sql-query>

</hibernate-mapping>
hibernate.cfg.xml
---------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

<mapping resource="User.hbm.xml"/>

<mapping resource="hibernate.queries.hbm.xml"/>

</session-factory>
</hibernate-configuration>

String username = "";
Session session = ;
Query query = session.getNamedQuery("QueryUser");
//Query query = session.getNamedQuery("SqlQueryUser");
query.setString("username", username);
List userList = query.list();

⑸ Hibernate 的HQL和sql有什麼區別

HQL:Hibernate Qusery Language,如果你已經熟悉它,就會發現它跟SQL非常相像。不過 你不要被表面的假象迷惑,HQL是面向對象的(OO,用生命的眼光看待每一個對象,他們是如此 鮮活)。如果你對JAVA和SQL語句有一定了解的話,那麼HQL對你簡直易如反掌,你完全可以利用在公車上的時間掌握它。 以下從幾個方面進行慢慢深入: 1。大小些敏感
大家知道Query是對大小寫不敏感的,但是在HQL(前面提到它是OO的)中那麼對象類的名稱和屬性確實大小寫敏感的(符合java編程語法)。
如:sElect cat.name from Cat as cat和select cat.name from Cat as cat是一樣的
但是:
sElect cat.name from CAT as cat和select cat.name from Cat as cat確實不一樣的。 2。from語句
最簡單的:
from eg.Cat
它只是簡單的返回所有eg.Cat的實例
通常我們此時會為eg.Cat其個別名,因為在query的其餘部分可能會用到(參看上邊關於大小寫
敏感時的例子情形),如:
from eg.Cat as cat 這里as可以省略。
上邊只是單表查詢,多表的情況如下寫法:
from eg.Cat,eg.Dog
from eg.Cat as cat,eg.Dog as dog 3。join相關
(inner) join
left (outer) join
right (outer) join
full join
HQL同樣對SQL中的這些特性支持
下面插播一個小話題,關於上邊的那些特性,我一直都沒怎麼用,今天既然說到這里,就想
把上邊的幾個特性的用法說一下,也算對自己的一個補充:
假設有兩個表:部門、員工,下面列舉一些數據:
員工(Employee):
ID Name DepNo
001 Jplateau 01
002 Jony 01
003 Camel 02
部門(Department):
ID Name
01 研發部
02 營銷部 在Hibernate中我們操縱的都是對象,所以我們操縱的是部門類和員工類
1).(inner) join
select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name
as name2 from Employee as employee join Department as department on employee.DepNo=
department.ID (注意到條件語句我用on 沒有用where)
那麼執行結果是什麼呢?
id1 name1 id2 name2
++++++++++++++++++++++++++++++++++++++
001 Jplateau 01 研發部
002 Jony 01 研發部 2).left (outer) join
select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name
as name2 from Employee as employee left join Department as department on employee.DepNo=
department.ID
那麼執行結果又該是什麼呢?
id1 name1 id2 name2
++++++++++++++++++++++++++++++++++++++
001 Jplateau 01 研發部
002 Jony 01 研發部
003 Camel null null
{就是說此時我要已第一個表的記錄多少為准,第二個表中沒有相應紀錄的時候填充null}
3). right (outer) join
select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name
as name2 from Employee as employee right join Department as department on employee.DepNo=
department.ID
那麼執行結果又該是什麼呢?
id1 name1 id2 name2
++++++++++++++++++++++++++++++++++++++
001 Jplateau 01 研發部
002 Jony 01 研發部
null null 02 營銷部
{就是說此時我要已第二個表的記錄多少為准,第一個表中沒有相應紀錄的時候填充null} 4。select語句
就是要確定你要從查詢中返回哪些對象或者哪些對象的屬性。寫幾個例子吧:
select employee form Employee as employee
select employee form Employee as employee where employee.Name like 'J%'
select employee.Name form Employee as employee where employee.Name like 'J%'
select employee.ID as id1,employee.Name as name1,department.ID as id2,department.Name
as name2 from Employee as employee right join Department as department on employee.DepNo=
department.ID select elements(employee.Name) from Employee as employee
(不明白elements到底是做什麼用的?望給於說明)
等等
5。數學函數
JDO目前好像還不支持此類特性。
avg(...), sum(...), min(...), max(...) count(*) count(...), count(distinct ...), count(all...) 其用法和SQL基本相同 select distinct employee.name from Employee as employee
select count(distinct employee.name),count(employee) from Employee as employee 6。polymorphism (暫時不知道如何解釋?)
from com.test.Animal as animal
不光得到所有Animal得實例,而且可以得到所有Animal的子類(如果我們定義了一個子類Cat)
一個比較極端的例子
from java.lang.Object as o
可以得到所有持久類的實例 7。where語句
定義查詢語句的條件,舉幾個例子吧:
from Employee as employee where employee.Name='Jplateau'
from Employee as employee where employee.Name like 'J%'
from Employee as employee where employee.Name like '%u'
在where語句中「=」不光可以比較對象的屬性,也可以比較對象,如:
select animal from com.test.Animal as animal where animal.name=dog 8。表達式 在SQL語句中大部分的表達式在HQL中都可以使用:
mathematical operators +, -, *, / binary comparison operators =, >=, <=, <>, !=, like logical operations and, or, not string concatenation || SQL scalar functions like upper() and lower() Parentheses ( ) indicate grouping in, between, is null JDBC IN parameters ? named parameters :name, :start_date, :x1 (這種應該是另一種"?"的變通解決方法) SQL literals 'foo', 69, '1970-01-01 10:00:01.0' Java public static final constants eg.Color.TABBY 其他不必解釋了,在這里我只想對查詢中的參數問題說明一下:
大家知道在SQL中進行傳遞參數進行查詢的時候,我們通常用PreparedStatement,在語句中寫一大堆的「?」,
在hql中也可以用這種方法,如:
List mates = sess.find(
"select employee.name from Employee as employee " +
"where employee.Name=? ",
name,
Hibernate.STRING
);
(說明:上面利用Session里的find方法,在hibernate的api Session中重載了很多find方法,它可以滿足你多種形式的查詢)
上邊是一個參數的情形,這種情況下緊接著引入參數和定義參數的類型,當為多個參數,調用另一個find方法,它的後兩個
參數都是數組的形式。 還有另外一種方法來解決上邊的問題,JDO也有這樣的方法,不過和hibernate的表現形式上有差別,但他們兩個骨子裡卻是
一樣的,如:
Query q = sess.createQuery("select employee.name from Employee as employee where employee.Name=:name");
q.setString("name", "Jplateau");
//當有多個參數的時候在此逐一定義
Iterator employees = q.iterate(); 9。order 語句
和sql語句沒什麼差別,如:
select employee.name from Employee as employee where employee.Name like 'J%' order by employee.ID desc (或者asc) 10。group by 語句
同樣和sql語句沒什麼差別,如: select employee.name,employee.DepNo from Employee as employee group by employee.DepNo select foo.id, avg( elements(foo.names) ), max( indices(foo.names) ) from eg.Foo foo group by foo.id
{Note: You may use the elements and indices constructs inside a select clause, even on databases with no subselects.}
誰幫我解釋一下上邊兩句,謝過! 11。子查詢
hibernate同樣支持子查詢,寫幾個例子: from eg.Cat as fatcat where fatcat.weight > ( select avg(cat.weight) from eg.DomesticCat cat )

⑹ Hibernate HQL 語句 執行 SQL語句

Long l = (Long)query.uniqueResult();

⑺ 我用的是SSH框架,我怎麼才能用程序確定hibernate執行hql或sql成功如hibernate的save、delete方法

你的意思是,你執行一個delete方法後,要返回一個提示嗎?

如果是這樣,你可以按照下面的例子寫:
public boolean delete(User user){
boolean flag = false;
try {
/*具體執行刪除操作,此處省略*/
flag = true;
}catch(Exception e) {
/*如果出現異常*/
flag=false;
}

return flag;
}

⑻ 在程序中將HQL轉換成sql(100分)

最好的方法是,去看hibernate 的源碼,然後,去修改那段 sysout...println(sql) 的sql返回到你的調用處,這樣就可以了。

你在修改後,從新編譯一下替換原有hibnate包中的類就ok了!!

祝你好運朋友!!!

⑼ 1,hibernate中用hql怎麼執行delete的sql語句

Session s = this.getHibernateTemplate().getSessionFactory().openSession();//獲取session

Transaction tx = s.beginTransaction(); //打開事務(針對讀資料庫)

String hql="delete .... where a=?...";//准備hql

s.createQuery(hql).setString("a",值).executeUpdate();//更新

tx.commit();//提交事務

s.close(); //關閉session

⑽ 在使用hibernate框架中是直接使用hql還是執行sql更好一點,如果是hql那麼多表查詢該如何實現

你好,你使用hibernate的目的就是避免想jdbc那樣的面向過程查詢。所以你首先要使用hql。你的實體類之間的關系已經在配置文件中或註解中寫好,所以多表查詢是可以直接用hql語句寫出來的,如:String hql="from Student s left join s.course c where s.sname='李曉梅'";
sql是用來解決較復雜的查詢語句,你記住這點就行了。

閱讀全文

與hibernatehql編譯sql相關的資料

熱點內容
工作三年的大專程序員 瀏覽:726
java畢業設計文獻 瀏覽:140
籌碼集中度指標源碼 瀏覽:477
listsortjava 瀏覽:183
plc閃光電路編程實例 瀏覽:297
socket編程試題 瀏覽:203
華為的伺服器怎麼設置從光碟機啟動 瀏覽:867
程序員真的累嗎 瀏覽:324
學信網app為什麼刷臉不了 瀏覽:872
天蠍vs程序員 瀏覽:991
單片機下載口叫什麼 瀏覽:188
程序員的道 瀏覽:926
雲伺服器不實名違法嗎 瀏覽:558
怎樣查看文件夾圖片是否重復 瀏覽:995
文件怎麼導成pdf文件 瀏覽:808
打開sql表的命令 瀏覽:103
安卓手機如何面部支付 瀏覽:38
天元數學app為什麼登錄不上去 瀏覽:824
明日之後為什麼有些伺服器是四個字 瀏覽:104
安卓系統l1是什麼意思 瀏覽:26