Hibernate中使用HQL语句删除数据

来源:互联网 发布:网络通信协议 编辑:程序博客网 时间:2024/05/14 22:35

http://justdo2008.iteye.com/blog/426272


在Hibernate 3中,增加了HQL删除语句,格式如下:
Delete FROM 表名 Where 列名=?

实例:

hibernate.cfg.xml:Hibernate环境配置文件

 

Xml代码  收藏代码
  1. <?xml version='1.0' encoding='UTF-8'?>    
  2. <!DOCTYPE hibernate-configuration PUBLIC     
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"     
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">    
  5.     
  6. <!-- Generated by MyEclipse Hibernate Tools.                   -->    
  7. <hibernate-configuration>    
  8.     
  9. <session-factory>    
  10.     <property name="connection.username">system</property>    
  11.     <property name="connection.url">    
  12.         jdbc:oracle:thin:@localhost:1521:MGC     
  13.     </property>    
  14.     <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>    
  15.     <property name="myeclipse.connection.profile">oracle</property>    
  16.     <property name="connection.password">admin</property>    
  17.     <property name="connection.driver_class">    
  18.         oracle.jdbc.driver.OracleDriver     
  19.     </property>    
  20.     <property name="show_sql">true</property>    
  21.     <mapping resource="mgc/hibernate/test/Member.hbm.xml" />    
  22.     
  23. </session-factory>    
  24.     
  25. </hibernate-configuration>    

 

Member.hbm.xml:数据库映射文件

 

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"     
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">    
  4. <!--     
  5.     Mapping file autogenerated by MyEclipse Persistence Tools    
  6. -->    
  7. <hibernate-mapping>    
  8.     <class name="mgc.hibernate.test.Member" table="MEMBER" schema="SYSTEM">    
  9.         <id name="id" type="java.lang.Long">    
  10.             <column name="ID" precision="22" scale="0" />    
  11.             <generator class="assigned"></generator>    
  12.         </id>    
  13.         <property name="username" type="java.lang.String">    
  14.             <column name="USERNAME" length="20" not-null="true" />    
  15.         </property>    
  16.         <property name="password" type="java.lang.String">    
  17.             <column name="PASSWORD" length="20" not-null="true" />    
  18.         </property>    
  19.     </class>    
  20. </hibernate-mapping>    

 

Member.java:POJO类

Java代码  收藏代码
  1. package mgc.hibernate.test;     
  2.     
  3. public class Member {     
  4.     
  5.     private long id ;     
  6.     private String username ;     
  7.     private String password ;     
  8.          
  9.     public long getId() {     
  10.         return id;     
  11.     }     
  12.     public void setId(long id) {     
  13.         this.id = id;     
  14.     }     
  15.     public String getUsername() {     
  16.         return username;     
  17.     }     
  18.     public void setUsername(String username) {     
  19.         this.username = username;     
  20.     }     
  21.     public String getPassword() {     
  22.         return password;     
  23.     }     
  24.     public void setPassword(String password) {     
  25.         this.password = password;     
  26.     }     
  27. }    

 

 MemberOperate.java:数据库操作类

Java代码  收藏代码
  1. package mgc.hibernate.test;     
  2.     
  3.     
  4. import java.util.Iterator;     
  5. import java.util.List;     
  6.     
  7. import org.hibernate.Query;     
  8. import org.hibernate.Session;     
  9. import org.hibernate.SessionFactory;     
  10. import org.hibernate.Transaction;     
  11. import org.hibernate.cfg.Configuration;     
  12.     
  13. public class MemberOperate {     
  14.     //所有的操作都是通过Session完成     
  15.     private Session session = null ;     
  16.          
  17.     //在构造方法中实例化Session对象     
  18.     public MemberOperate() {     
  19.         //找到Hibernate配置     
  20.         Configuration config = new Configuration().configure() ;     
  21.         //从配置中取出SessionFactory     
  22.         SessionFactory factroy = config.buildSessionFactory() ;     
  23.         //取出一个Session     
  24.         this.session = factroy.openSession() ;     
  25.     }     
  26.          
  27.     //使用HQL语句删除数据     
  28.     public void delete(int id) {     
  29.         Transaction tran = this.session.beginTransaction() ;     
  30.         String hql = "Delete FROM Member Where id=?" ;     
  31.         Query q = this.session.createQuery(hql) ;     
  32.         q.setInteger(0, id) ;     
  33.         q.executeUpdate() ;     
  34.         tran.commit() ;     
  35.     }     
  36. }    

 

 

TestDel02.java:应用程序

Java代码  收藏代码
  1. package mgc.hibernate.test;     
  2.     
  3. public class TestDel02 {     
  4.     
  5.     /**   
  6.      * @param args   
  7.      */    
  8.     public static void main(String[] args) {     
  9.         //实例化MemberOperate对象     
  10.         MemberOperate mo = new MemberOperate() ;     
  11.         mo.delete(4) ;     
  12.     }     
  13.     
  14. }