您现在的位置:首页 > IT认证 > JAVA >

Java基础知识:Hibernate本地SQL查询


本地SQL查询来完善HQL不能涵盖所有的查询特性

  下面通过例子来理解本地SQL。

  例子:查询用户和租房的信息

  1.配置文件

  hibernate.cfg.xml

  <?xml version='1.0' encoding='utf-8'?>

  <!DOCTYPE hibernate-configuration PUBLIC

  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

  "/hibernate-configuration-3.0.dtd">

  <hibernate-configuration>

  <session-factory>

  <!-- Database connection settings -->

  <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

  <property name="connection.url">jdbc:oracle:thin:@OWEYOJ5DU7AAHZZ:1521:ORCL</property>

  <property name="ername">jbit</property>

  <property name="connection.password">bdqn</property>

  <!-- JDBC connection pool (use the built-in) -->

  <property name="connection.pool_size">1</property>

  <!-- SQL dialect -->

  <property name="dialect">org.hibernate.dialect.OracleDialect</property>

  <!-- Enable Hibernate's automatic session context management -->

  <property name="current_session_context_class">thread</property>

  <!-- Disable the second-level cache -->

  <!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> -->

  <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property>

  <!-- Drop and re-create the database schema on startup -->

  <property name="hbm2ddl.auto">update</property>

  <mapping resource="cn/jbit/hibernate/entity/User.hbm.xml" />

  <mapping resource="cn/jbit/hibernate/entity/House.hbm.xml" />

  <mapping resource="cn/jbit/hibernate/entity/Street.hbm.xml" />

  <mapping resource="cn/jbit/hibernate/entity/Type.hbm.xml" />

  <mapping resource="cn/jbit/hibernate/entity/District.hbm.xml" />

  </session-factory>

  </hibernate-configuration>

  2.hibernate工具类

  HibernateUtil.java

  package cn.jbit.hibernate.util;

  import org.hibernate.HibernateException;

  import org.hibernate.Session;

  import org.hibernate.SessionFactory;

  import org.nfiguration;

  /*

  * hibernate工具类

  */

  public class HibernateUtil {

  private static Configuration configuration;

  private static final SessionFactory sessionFactory;

  static{

  try { configuration=new Configuration();

  nfigure();

  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> 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) {

  ername = username;

  }

  public String getIsadmin() {

  return isadmin;

  }

  public void setIsadmin(String isadmin) {

  this.isadmin = isadmin;

  }

  public Set<House> getHouse() {

  return house;

  } public void setHouse(Set<House> 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;

  private Date date

相关文章

无相关信息
更新时间2022-09-16 10:07:22【至顶部↑】
联系我们 | 邮件: | 客服热线电话:4008816886(QQ同号) | 

付款方式留言簿投诉中心网站纠错二维码手机版

电话:
付款方式   |   给我留言   |   我要纠错   |   联系我们