Hibernate 本地SQL查询 (一)

2014-11-24 11:10:33 · 作者: · 浏览: 2

本地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">


oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@OWEYOJ5DU7AAHZZ:1521:ORCL
jbit
bdqn


1


org.hibernate.dialect.OracleDialect


thread



true


update






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 house;

//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 getHouse() {
return house;
}
public void setHouse(Set house) {
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;