Hibernate中使用HQL语句删除数据
来源:互联网 发布:网络通信协议 编辑:程序博客网 时间:2024/05/14 22:35
http://justdo2008.iteye.com/blog/426272
在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) ;
- }
- }
- Hibernate中使用HQL语句删除数据
- Hibernate中HQL使用SELECT语句
- Hibernate中HQL语句的使用
- Hibernate中HQL语句的使用
- Hibernate中HQL语句的使用
- Hibernate 中HQL语句
- Hibernate 中HQL语句
- Hibernate中HQL语句
- hibernate中HQL语句
- hibernate中hql语句
- Hibernate 中HQL语句
- Hibernate中HQL语句
- Hibernate(3)-使用HQL语句进行数据查询
- hibernate中hql语句大全
- Hibernate HQL语句使用总结
- 如何在hibernate+mySQL中使用HQL的cast语句
- 在hibernate中hql语句使用连接查询
- hibernate中使用sql语句删除
- 对i2c子系统的理解
- 关于Java的ORM及 PHP直接面向数据库关系 模式的思考
- javaScript中的对象
- java uuid生成方法
- 《推荐系统实践》阅读笔记一 评价标准和冷启动问题
- Hibernate中使用HQL语句删除数据
- 以太网MAC帧格式
- ,《计算机网络》这门课为何如此之难?
- 一切都是View
- 值得关注的博客和网站
- Java IO流学习总结
- ubunt 12.10 vi编辑器 中文乱码 和ubuntu显示中文的办法 解决办法
- 值得期待的cocos2d-x 3.0
- 怎样花两年时间去面试一个人