射精一区欧美专区|国产精品66xx|亚洲视频一区导航|日韩欧美人妻精品中文|超碰婷婷xxnx|日韩无码综合激情|特级黄片一区二区|四虎日韩成人A√|久久精品内谢片|亚洲成a人无码电影

您現(xiàn)在的位置:首頁(yè) > IT認(rèn)證 > JAVA >

Java基礎(chǔ)知識(shí):Hibernate本地SQL查詢


本地SQL查詢來(lái)完善HQL不能涵蓋所有的查詢特性

  下面通過(guò)例子來(lái)理解本地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();

  }

  }

  實(shí)體類

  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;

  }

  }

  實(shí)體類

  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

相關(guān)文章

無(wú)相關(guān)信息
更新時(shí)間2022-09-16 10:07:22【至頂部↑】
聯(lián)系我們 | 郵件: | 客服熱線電話:4008816886(QQ同號(hào)) | 

付款方式留言簿投訴中心網(wǎng)站糾錯(cuò)二維碼手機(jī)版

電話:
付款方式   |   給我留言   |   我要糾錯(cuò)   |   聯(lián)系我們