本地SQL查询来完善HQL不能涵盖所有的查询特性
下面通过例子来理解本地SQL。
例子:查询用户和租房的信息
1.配置文件
hibernate.cfg.xml
< xml version='1.0' encoding='utf-8' >
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
2.hibernate工具类
HibernateUtil.java
package cn.jbit.hibernate.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/*
* hibernate工具类
*/
public class HibernateUtil {
private static Configuration configuration;
private static final SessionFactory sessionFactory;
static{
try {
configuration=new Configuration();
configuration.configure();
sessionFactory=configuration.buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public Session getSession() throws HibernateException{
return getSessionFactory().getCurrentSession();
}
}
实体类
User,java
package cn.jbit.hibernate.entity;
import java.util.Set;
public class User implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String password;
private String telephone;
private String username;
private String isadmin;
private Set
//get&set方法
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getIsadmin() {
return isadmin;
}
public void setIsadmin(String isadmin) {
this.isadmin = isadmin;
}
public Set
return house;
}
public void setHouse(Set
this.house = house;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
实体类
House.java
package cn.jbit.hibernate.entity;
import java.util.Date;
public class House {
private Integer id;
private Integer type_id;
private Integer user_id;
private Integer street_id;
private String description;