在Hibernate 3中,增加了HQL删除语句,格式如下:
Delete FROM 表名 Where 列名=?
实例:
hibernate.cfg.xml:Hibernate环境配置文件
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="connection.username">system</property> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:MGC </property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="myeclipse.connection.profile">oracle</property> <property name="connection.password">admin</property> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="show_sql">true</property> <mapping resource="mgc/hibernate/test/Member.hbm.xml" /> </session-factory> </hibernate-configuration>
Member.hbm.xml:数据库映射文件
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="mgc.hibernate.test.Member" table="MEMBER" schema="SYSTEM"> <id name="id" type="java.lang.Long"> <column name="ID" precision="22" scale="0" /> <generator class="assigned"></generator> </id> <property name="username" type="java.lang.String"> <column name="USERNAME" length="20" not-null="true" /> </property> <property name="password" type="java.lang.String"> <column name="PASSWORD" length="20" not-null="true" /> </property> </class> </hibernate-mapping>
Member.java:POJO类
package mgc.hibernate.test;
public class Member {
private long id ;
private String username ;
private String password ;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
MemberOperate.java:数据库操作类
package mgc.hibernate.test;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class MemberOperate {
//所有的操作都是通过Session完成
private Session session = null ;
//在构造方法中实例化Session对象
public MemberOperate() {
//找到Hibernate配置
Configuration config = new Configuration().configure() ;
//从配置中取出SessionFactory
SessionFactory factroy = config.buildSessionFactory() ;
//取出一个Session
this.session = factroy.openSession() ;
}
//使用HQL语句删除数据
public void delete(int id) {
Transaction tran = this.session.beginTransaction() ;
String hql = "Delete FROM Member Where id=?" ;
Query q = this.session.createQuery(hql) ;
q.setInteger(0, id) ;
q.executeUpdate() ;
tran.commit() ;
}
}
TestDel02.java:应用程序
package mgc.hibernate.test;
public class TestDel02 {
/**
* @param args
*/
public static void main(String[] args) {
//实例化MemberOperate对象
MemberOperate mo = new MemberOperate() ;
mo.delete(4) ;
}
}