熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate 命名查詢NamedQuery

2022-06-13   來源: Java開源技術 

  例子  使用命名查詢實現多條件對租房信息進行模糊查詢

  房屋實體類

  Housejava

  package cnjbithouserentbean;

  import javautilDate;

  public class House implements javaioSerializable {

  private Integer id;

  private User user;

  private Type type;

  private Street street;

  private String title;

  private String description;

  private Double price;

  private Date date;

  private Integer floorage;

  private String contact;

  public Integer getId() {

  return thisid;

  }

  public void setId(Integer id) {

  thisid = id;

  }

  public User getUser() {

  return thisuser;

  }

  public void setUser(User user) {

  thisuser = user;

  }

  public Type getType() {

  return thistype;

  }

  public void setType(Type type) {

  thistype = type;

  }

  public Street getStreet() {

  return thisstreet;

  }

  public void setStreet(Street street) {

  thisstreet = street;

  }

  public String getTitle() {

  return thistitle;

  }

  public void setTitle(String title) {

  thistitle = title;

  }

  public String getDescription() {

  return thisdescription;

  }

  public void setDescription(String description) {

  thisdescription = description;

  }

  public Double getPrice() {

  return thisprice;

  }

  public void setPrice(Double price) {

  thisprice = price;

  }

  public Date getDate() {

  return thisdate;

  }

  public void setDate(Date date) {

  thisdate = date;

  }

  public Integer getFloorage() {

  return thisfloorage;

  }

  public void setFloorage(Integer floorage) {

  thisfloorage = floorage;

  }

  public String getContact() {

  return ntact;

  }

  public void setContact(String contact) {

  ntact = contact;

  }

  }

  配置映射文件

  Househbmxml

  <?xml version= encoding=utf?>

  <!DOCTYPE hibernatemapping PUBLIC //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <!

  Mapping file autogenerated by MyEclipse Persistence Tools

  >

  <hibernatemapping>

  <class name=cnjbithouserentbeanHouse table=house lazy=false schema=jbit>

  <id name=id type=javalangInteger>

  <column name=id />

  <generator class=sequence >

  <param name=sequence>SEQ_ID</param>

  </generator>

  </id>

  <manytoone name=user  class=cnjbithouserentbeanUser cascade=none fetch=join>

  <column name=user_id />

  </manytoone>

  <manytoone name=type class=cnjbithouserentbeanType cascade=none fetch=join>

  <column name=type_id />

  </manytoone>

  <manytoone name=street class=cnjbithouserentbeanStreet cascade=none fetch=join>

  <column name=street_id />

  </manytoone>

  <property name=title type=javalangString>

  <column name=title length= notnull=false />

  </property>

  <property name=description type=text lazy=false>

  <column name=description />

  </property>

  <property name=price type=javalangDouble>

  <column name=price precision= scale= notnull=false />

  </property>

  <property name=date type=javautilDate>

  <column name=pubdate length= />

  </property>

  <property name=floorage type=javalangInteger>

  <column name=floorage length= />

  </property>

  <property name=contact type=javalangString>

  <column name=contact length= />

  </property>

  </class>

  <在映射文件中<query>元素用於定義一個HQL查詢語句它和<class>元素並列給HQL語句命名queryHouse以<![CDATA[ HQL ]]>方式保存HQL語句在程序中通過Session對象的getNamedQuery()方法獲取該查詢語句 >

  <query name=queryHouse>

  <![CDATA[

  from House where (title like :title) and

  (price between :low_price and :high_price) and

  (street_id like :street_id) and (type_id like :type_id) and

  (floorage between :small_floorage and :big_floorage)

  ]]>

  </query>

  </hibernatemapping>

  封裝查詢參數

  QueryPropertiesjava

  package cnjbithouserentutil;

  import javautilDate;

  public class QueryProperties {

  private String title;

  private Double high_price;

  private Double low_price;

  private Date start_date;

  private Date end_date;

  private String type_id;

  private String street_id;

  private Integer small_floorage;

  private Integer big_floorage;

  public Double getHigh_price() {

  return high_price;

  }

  public void setHigh_price(Double high_price) {

  thishigh_price = high_price;

  }

  public Double getLow_price() {

  return low_price;

  }

  public void setLow_price(Double low_price) {

  thislow_price = low_price;

  }

  public String getType_id() {

  return type_id;

  }

  public void setType_id(String type_id) {

  thistype_id = type_id;

  }

  public String getStreet_id() {

  return street_id;

  }

  public void setStreet_id(String street_id) {

  thisstreet_id = street_id;

  }

  public Date getStart_date() {

  return start_date;

  }

  public void setStart_date(Date start_date) {

  thisstart_date = start_date;

  }

  public Date getEnd_date() {

  return end_date;

  }

  public void setEnd_date(Date end_date) {

  thisend_date = end_date;

  }

  public Integer getSmall_floorage() {

  return small_floorage;

  }

  public void setSmall_floorage(Integer small_floorage) {

  thissmall_floorage = small_floorage;

  }

  public Integer getBig_floorage() {

  return big_floorage;

  }

  public void setBig_floorage(Integer big_floorage) {

  thisbig_floorage = big_floorage;

  }

  public String getTitle() {

  return title;

  }

  public void setTitle(String title) {

  thistitle = title;

  }

  }

  測試代碼

  Testjava

  import javautilIterator;

  import javautilList;

  import orghibernateHibernateException;

  import orghibernateQuery;

  import orghibernateSession;

  import orghibernateSessionFactory;

  import orghibernateTransaction;

  import cnjbithibernateentityHouse;

  import cnjbithibernateentityQueryProperties;

  import cnjbithibernateentityUser;

  import cnjbithibernateutilHibernateUtil;

  public class Test {

  /**

  * @param args

  */

  public static void main(String[] args) {

  HibernateUtil u= new HibernateUtil();

  SessionFactory sf = null;

  Session session =null;

  Transaction tx=null;

  try{

  session=ugetSession();

  tx=sessionbeginTransaction();

  Query query =sessiongetNamedQuery(queryHouse);//獲取命名查詢語句

  //給封裝參數類添加參數值

  QueryProperties qp = new QueryProperties();

  qpsetTitle(豪放);

  qpsetLow_price(new Integer());

  qpsetHigh_price(new Integer());

  qpsetStreet_id();

  qpsetType_id();

  qpsetSmall_floorage(new Integer());

  qpsetBig_floorage(new Integer());

  querysetProperties(qp);

  List result =querylist();

  Iterator it = erator();

  if(ithasNext()){

  House house=(House)itnext();

  Systemoutprintln(housegetTitle()+ +housegetPrice());

  }*/

  //String hql = from User u left join fetch uhouse h ;

  String hql=from District d inner join dstreet s;

  Query query = sessioncreateQuery(hql);

  List list=querylist();

  }catch(HibernateException e){

  eprintStackTrace();

  }

  }

  }

  注意這裡沒有寫出配置文件和創建工廠會話等代碼


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28724.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.