视窗
loading...
您现在的位置:首页 > 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

闂傚倷绶氬ḿ褍螞瀹€鍕;闁跨噦鎷�

闂傚倷绶氬ḿ褍螞瀹€鍕;闁跨噦鎷�

闂傚倷绶氬ḿ褍螞濞嗘挸绀夐柡鍥ュ灩閸屻劑鏌曢崼婵囧閻庢艾顭烽弻銊モ攽閸℃ê鐝旂紓浣瑰敾缂嶄線寮婚敓鐘茬妞ゆ劧瀵岄埀顒侇殜閺岀喖鎽庨崒姘ギ闂佽鍟崶褔鍞堕梺缁樻煥閹芥粎绮旈鍕厽闁绘ê寮堕幖鎰版煟鎺抽崝灞藉祫闂佺鎻粻鎴g箽闂備浇娉曢崳锕傚箯閿燂拷
闂傚倷绀侀幖顐︽偋濠婂牆绀堟繛鎴欏灪閸嬬喐銇勯弽顐沪闁绘帡绠栭弻鏇熺箾閸喖濮庨梺璇叉唉椤曆嗗絹闂佹悶鍎荤徊鑺ユ櫠閹绘崡褰掓偂鎼淬垹娈楅悗娈垮枛閻栧吋淇婇悜鑺ユ櫆闁告挆鍐帗婵犵數鍋涢悺銊у垝瀹€鈧懞閬嶆嚃閳哄嫬小婵炲鍘ч悺銊╁吹閸岀偞鐓涢柛鎰╁妼閳ь剝宕电划鏃囥亹閹烘挾鍙嗗┑鐐村灱濞呮洜鈧熬鎷�闂備浇顕уù鐑藉极閹间降鈧焦绻濋崑顖氱秺瀹曞崬螣鐠囧樊娼梻浣风串缁蹭粙寮甸鍕仼闁告鍊戞惔銊ョ倞闁挎繂鎳庨埛澶嬬箾鐎电ǹ甯堕柟铏耿閻涱喚鈧綆鍠栫粻锝嗙節婵犲倸顏柟鏋姂濮婃椽宕ㄦ繝鍐f嫻缂備礁顑嗙敮锟犲箚閸ャ劌顕遍柡澶嬪灥閸炪劑姊洪幖鐐插姌闁告柨绉归敐鐐哄冀椤儱閰e畷鍫曟晲閸涱厸鎷ゅ┑鐐差嚟閸樠囧箠濮椻偓楠炲棝宕橀鑲╊槹濡炪倖鍔戦崹娲儊閺冣偓缁绘繈濮€閿濆棛銆愰柣搴㈣壘閹芥粌危閹扮増鏅搁柨鐕傛嫹闂傚倷鑳堕幊鎾绘偤閵娾晛鍨傞柛鎰ゴ閸亪鏌熺€电ǹ小闁绘帒锕ら埞鎴︽偐閸欏鎮欏┑鐐叉噷閸ㄥ綊婀侀梺鎸庣箓濡盯鎯屾惔銊︾厓鐟滄粓宕滃▎鎾崇疇闁归偊鍏橀弸鏃堟煙鏉堥箖妾柛瀣儔閺屾盯顢曢妶鍛€剧紓鍌氱М閸嬫捇姊绘担鐟扮亰闁绘帪绠撴俊鐢告倷閻㈢數顦梺鍝勫暙閻楀棛鐥閺屾盯骞囬娑氥€婄紓浣插亾闁跨噦鎷�闂備浇顕х花鑲╁緤婵犳熬缍栧鑸靛姇閸ㄥ倹绻濇繝鍌氼伀妞も晠鏀遍妵鍕箳閹存繃娈茬紓浣风贰閸o絽顕i崼鏇熷€烽柛顭戝亝閻濐亜鈹戦悙鑼闁搞劌澧庣划姘舵焼瀹ュ懐鍔撮梺鍛婂壃閸涱垼妲繝鐢靛Х椤d粙鍩€椤掆偓閸熷潡鍩€椤掑倹鍤€闁挎洏鍨洪幏鍛村礈閹绘帗顓块梻浣呵归張顒傛崲瀹ュ鑸归柟缁㈠枟閻撴瑩鎮楀☉娆嬬細濠⒀屽墯缁绘繈鍩€椤掍焦濯撮柛锔诲幘閹虫繈姊洪崜鑼帥闁稿鎳愮槐鐐哄焵椤掑嫭鈷戦柣鐔稿閹界娀鏌eΔ鍐ㄤ户闁瑰箍鍨归~婵嬵敄閼恒儳浜欓梻浣告惈濞诧箓鎯岄鐐床闁糕剝绋掗悡娆愩亜閹炬鍟版禒鏉戭渻閵堝棙澶勯柛鎾跺枎閻g兘鏁愰崱娆戠槇闂佸憡鍔忛弲鈺佄i鐐粹拺闁奸€涘嵆閸濈儤鎱ㄦ繝鍌ょ吋闁哄苯鐗撻獮姗€顢欓懖鈺婂悈闂備胶鎳撻悺銊у垝瀹ュ洤鍨濋柨鏇炲€归悡銉︾箾閹寸倖鎴濓耿閻楀牏绡€闁逞屽墴楠炲秹顢欓幆褍瑙︽繝鐢靛仜濡霉妞嬪海鐜绘俊銈呭暟绾惧ジ鏌¢崒娑卞劌闁稿骸绻掗埀顒冾潐閹哥ǹ螞濠靛棛鏆︽慨妯垮煐閸嬫劗绱撴担璐細鐟滅増鍨垮娲箰鎼粹€虫灆闂佺懓鍤栭幏锟�闂備浇顕уù鐑藉极閹间礁绠犻柟鎹愬煐閺嗘粍銇勯幇鍓佺暠缂佲偓閸℃ḿ绠鹃柟瀵镐紳椤忓牆鏋侀柛顐f礃閸婄數鐥鐐村婵炲吋鍔欓弻娑㈠Ω閿斿墽鐓佺紓浣稿€圭敮锟犮€佸Δ浣瑰缂佸鏅濋锔解拺閻熸瑥瀚欢鑼磼缂佹ê鐏寸€殿噮鍋婇、娆撴偩瀹€濠冪カ婵犳鍠楅妵娑㈠磻閹剧粯鐓冪憸婊堝礈濞嗘挸纾归柛婵勫劤缁€濠囨倵閿濆骸鏋熼柛搴$Ч閺屾盯寮撮妸銉ョ瑢閻熸粎澧楃敮妤呮偂閳ь剙顪冮妶鍡楃瑨闁挎洩濡囩划鍫ユ晸閿燂拷闂傚倷娴囨竟鍫熴仈缁嬫娼栧┑鐘崇閻掗箖鏌熺紒銏犳灈婵☆偅锕㈤弻锝夋偄缁嬫妫嗙紒缁㈠幐閸嬫捇姊绘担鐟邦嚋缂佸甯掗悾婵嬪箹娴e摜锛涢梺鍝勭Р閸斿酣銆呴悜鑺ョ叆闁绘洖鍊圭€氾拷:webmaster@jscj.com闂傚倷绶氬ḿ褍螞瀹€鍕;闁瑰墽绮悡鐔搞亜椤愵偄骞樼紒浣哄厴閺岋綁鏁傜捄銊х厯闂佽桨绀佺粔褰掑极閹剧粯鏅搁柨鐕傛嫹4008816886

相关文章

无相关信息
更新时间2022-09-16 10:07:22【至顶部↑】
联系我们 | 邮件: webmaster@jscj.com | 客服热线电话:4008816886(QQ同号) |  濠电姷鏁告慨鐑藉极閹间礁纾块柟瀵稿Х缁€濠囨煃瑜滈崜姘跺Φ閸曨垰鍗抽柛鈩冾殔椤忣亪鏌涘▎蹇曠闁哄矉缍侀獮鍥敋閸涱喗鐦滈梻浣呵归鍡涘箲閸ヮ剚鍋樻い鏇楀亾鐎规洖缍婇、娆撳矗閵壯勶紡婵犵數濮烽弫鍛婃叏椤撱垹纾婚柟鍓х帛閻撴洟鏌曟径瀣仴闁兼椿鍨伴埢鎾活敃閵忊晜瀵岄梺闈涚墕濡瑩藟閸℃ǜ浜滈柡鍌氬皡閸旂喖鏌熼獮鍨伈闁轰焦鎹囬弫鎾绘晸閿燂拷

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

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