本地SQL查詢來完善HQL不能涵蓋所有的查詢特性
下面通過例子來理解本地SQL
例子
hibernate
<?xml version=
<!DOCTYPE hibernate
<hibernate
<session
<!
<property name=
<property name=
<property name=
<property name=
<!
<property name=
<!
<property name=
<!
<property name=
<!
<!
<!
<property name=
<!
<property name=
<mapping resource=
<mapping resource=
<mapping resource=
<mapping resource=
<mapping resource=
</session
</hibernate
HibernateUtil
package cn
import org
import org
import org
import org
/*
* hibernate工具類
*/
public class HibernateUtil {
private static Configuration configuration;
private static final SessionFactory sessionFactory;
static{
try {
configuration=new Configuration();
nfigure();
sessionFactory=configuration
}
catch (Throwable ex) {
// Make sure you log the exception
System
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public Session getSession() throws HibernateException{
return getSessionFactory()
}
}
實體類
User
package cn
import java
public class User implements java
private static final long serialVersionUID =
private Integer id;
private String name;
private String password;
private String telephone;
private String username;
private String isadmin;
private Set<House> house;
//get&set方法
public Integer getId() {
return id;
}
public void setId(Integer id) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this
}
public String getIsadmin() {
return isadmin;
}
public void setIsadmin(String isadmin) {
this
}
public Set<House> getHouse() {
return house;
}
public void setHouse(Set<House> house) {
this
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
實體類
House
package cn
import java
public class House {
private Integer id;
private Integer type_id;
private Integer user_id;
private Integer street_id;
private String description;
private Date date;
private Integer price;
private String contact;
private Integer floorage;
private String title;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this
}
public Integer getType_id() {
return type_id;
}
public void setType_id(Integer type_id) {
this
}
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this
}
public Integer getStreet_id() {
return street_id;
}
public void setStreet_id(Integer street_id) {
this
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
ntact = contact;
}
public Integer getFloorage() {
return floorage;
}
public void setFloorage(Integer floorage) {
this
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this
}
}
House
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<column name=
<generator class=
</generator>
</id>
<property name=
<column name=
</property>
<property name=
<column name=
</property>
<property name=
<column name=
</property>
<property name=
<column name=
</property>
<property name=
<column name=
</property>
<property name=
<column name=
</property>
<property name=
<column name=
</property>
<property name=
<column name=
</property>
<property name=
<column name=
</property>
</class>
</hibernate
User
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<column name=
<generator class=
<param name=
</generator>
</id>
<property name=
<column name=
</property>
<property name=
<column name=
</property>
<property name=
<column name=
</property>
<property name=
<column name=
</property>
<property name=
<column name=
</property>
<set name=
<key>
<column name=
</key>
<one
</set>
</class>
<!
使用<sql
指定別名
<sql
<![CDATA[
select {u
]]>
<return alias=
<return alias=
</sql
</hibernate
測試類
Test
import java
import java
import org
import org
import org
import org
import org
import cn
import cn
import cn
import cn
public class Test {
public static void main(String[] args) {
HibernateUtil u= new HibernateUtil();
SessionFactory sf = null;
Session session =null;
Transaction tx=null;
try{
session=u
tx=session
//本地SQL查詢
Query query=session
List result=query
Iterator it=erator();
while(it
Object[] results=(Object[])it
User user =(User)results[
House house=(House)results[
System
}
}catch(HibernateException e){
e
}
}
}
如果不在User
執行本地sql查詢將不是使用Query接口了
在使用這個方法時
例如
Test
import java
import java
import org
import org
import org
import org
import org
import cn
import cn
import cn
import cn
public class Test
public static void main(String[] args) {
HibernateUtil u= new HibernateUtil();
SessionFactory sf = null;
Session session =null;
Transaction tx=null;
try{
session=u
tx=session
//本地SQL查詢
//sql語句中的u是sql中表Users表名
String sql=
SQLQuery query=session
List result=query
Iterator it=erator();
while(it
Object[] results=(Object[])it
User user =(User)results[
House house=(House)results[
System
}
}catch(HibernateException e){
e
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28346.html