`

hibernate删除的理解

    博客分类:
  • java
阅读更多

customer2的配置文件

<?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="com.bsw.job.Customer2" table="customer2" catalog="hibernateex">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" />
</property>
<property name="email" type="java.lang.String">
<column name="email" length="30" />
</property>
<property name="address" type="java.lang.String">
<column name="address" length="30" />
</property>
</class>
</hibernate-mapping>

Customer2DAO 类

package com.bsw.job;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import com.sun.org.apache.bcel.internal.generic.GETSTATIC;

/**
* Data access object (DAO) for domain model class Customer2.
*
* @see com.bsw.job.Customer2
* @author MyEclipse Persistence Tools
*/

public class Customer2DAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(Customer2DAO.class);

public List selectCustomer(String name){
String hql="from Customer2 where name =:name order by age";
Query query =getSession().createQuery(hql);
query.setParameter("name", name);
List list=query.list();
return list;

}

public List selectAllCustomer(){
String hql="from Customer2 order by age";
Query query =getSession().createQuery(hql);

List list=query.list();
return list;

}

public void deleteCustomer(Customer2 customer){
Transaction tr=getSession().beginTransaction();
getSession().delete(customer);
tr.commit();
getSession().close();
}

public void updateCustomer(Customer2 customer){
Transaction tr=getSession().beginTransaction();
getSession().saveOrUpdate(customer);
tr.commit();
getSession().close();
}

public void addCustomer(Customer2 customer){
Transaction tr=getSession().beginTransaction();
getSession().save(customer);
tr.commit();
getSession().close();
}
public void save(Customer2 transientInstance) {
log.debug("saving Customer2 instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

public void delete(Customer2 persistentInstance) {
log.debug("deleting Customer2 instance");
try {
Transaction tr=getSession().beginTransaction();
getSession().delete(persistentInstance);
tr.commit();
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}

public Customer2 findById(java.lang.Integer id) {
log.debug("getting Customer2 instance with id: " + id);
try {
Customer2 instance = (Customer2) getSession().get(
"com.bsw.job.Customer2", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}

public List findByExample(Customer2 instance) {
log.debug("finding Customer2 instance by example");
try {
List results = getSession().createCriteria("com.bsw.job.Customer2")
.add(Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

public List findByProperty(String propertyName, Object value) {
log.debug("finding Customer2 instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Customer2 as model where model."
+ propertyName + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}

public List findAll() {
log.debug("finding all Customer2 instances");
try {
String queryString = "from Customer2";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

public Customer2 merge(Customer2 detachedInstance) {
log.debug("merging Customer2 instance");
try {
Customer2 result = (Customer2) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public void attachDirty(Customer2 instance) {
log.debug("attaching dirty Customer2 instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void attachClean(Customer2 instance) {
log.debug("attaching clean Customer2 instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}

测试类

package com.bsw.test;

import java.util.List;

import com.bsw.job.Customer2;
import com.bsw.job.Customer2DAO;

public class TestCustomer2 {
public static void main(String[] args) {
Customer2DAO dao=new Customer2DAO();
List<Customer2> list=(List<Customer2>)dao.selectAllCustomer();
System.out.println("按年龄从小到大排:");
System.out.println("用户ID "+"姓名 "+"年龄 "+"邮箱 "+"地址");
//for(Customer2 customer:list){
Customer2 customer=new Customer2();
customer.setAddress("上帝");
customer.setAge(25);
dao.addCustomer(customer);
Customer2 customer1=new Customer2();
//dao.getSession().get("customer1", 7);
customer1.setAddress("上帝");//在此处用这样就删不掉,还会报错,因为没有持久化,但如果用customer1.setId(7);的话就不会报错,因为在session.delete()方法中可以根据id把你所传的customer1给内部持久化。
dao.deleteCustomer(customer1);
//}
}
}

分享到:
评论

相关推荐

    Hibernate+中文文档

    19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...

    hibernate3.2中文文档(chm格式)

    19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...

    HibernateAPI中文版.chm

    19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...

    hibernate 教程

    理解集合的性能 14.1.1. 分类 14.1.2. Lists, maps 和sets用于更新效率最高 14.1.3. Bag和list是反向集合类中效率最高的 14.1.4. 一次性删除(One shot delete) 14.2. 用于延迟装载的代理 14.3. ...

    struts+hibernate实现的网络购物系统下载.zip

    这个程序时学习struts+hibernate的比较好的例子,这个系统是我在网上找到的。不过不是用的tomcat... 为了减少网络传输,我把系统运行依赖的struts,hibernate等的jar包都删除了,所以请朋友们运行时自行加上,谢谢!

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     8.2 理解Session的缓存  8.2.1 Session的缓存的作用  8.2.2 脏检查及清理缓存的机制  8.3 Java对象在Hibernate持久化层的状态  8.3.1 临时对象的特征  8.3.2 持久化对象的特征  8.3.3 被删除对象的特征  ...

    Hibernate中文详细学习文档

    19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...

    Hibernate 中文 html 帮助文档

    19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...

    最全Hibernate 参考文档

    19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...

    精通hibernate:对象持久化技术孙卫琴第二版part2

    本章还将介绍通过Hibernate API来保存、修改和删除具有关联关系的对象的方法。 7.1 建立多对一的单向关联关系 148 7.1.1 [many-to-one]元素的not-null属性 153 7.1.2 级联保存和更新 155 7.2 映射一对多双向关联...

    Hibernate持久化对象的生命周期

    Hibernate持久化对象的生命周期 持久化对象的状态: 瞬时对象(Transient Objects)持久化对象(Persist Objects)、离线对象(Detached ...在Hibernate应用中Java对象的状态 Session的保存、更新、删除、查询方法:

    hibernate 体系结构与配置 参考文档(html)

    删除持久对象 10.9. 在两个不同数据库间复制对象 10.10. Session刷出(flush) 10.11. 传播性持久化(transitive persistence) 10.12. 使用元数据 11. 事务和并发 11.1. Session和事务范围(transaction scope) ...

    Hibernate教程

    20.5. 理解集合性能(Understanding Collection performance) 20.5.1. 分类(Taxonomy) 20.5.2. Lists, maps 和sets用于更新效率最高 20.5.3. Bag和list是反向集合类中效率最高的 20.5.4. 一次性删除(One ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...

    基于Struts2+Hibernate+Spring+MySQL的B2C网站管理系统购物网项目源码

    因此想要下载此项目源码的读者必须用10积分才能下载,希望读者能理解.也希望你能从这个代码中能够很好的利用它. 注:里面的数据库文件都放在datebase目录下.(由于此处描述不支持图片功能,因此不能很好的展示效果图)...

    hibernate

    理解集合的性能 14.1.1. 分类 14.1.2. Lists, maps 和sets用于更新效率最高 14.1.3. Bag和list是反向集合类中效率最高的 14.1.4. 一次性删除(One shot delete) 14.2. 用于延迟装载的代理 14.3. ...

    基于JAVA毕业设计-JAVA struts+hibernate实现的网络购物系统.zip

    基于JAVA毕业设计-JAVA struts+hibernate实现的网络购物系统.zip 这个程序时学习struts+hibernate的... 为了减少网络传输,我把系统运行依赖的struts,hibernate等的jar包都删除了,所以请朋友们运行时自行加上,谢谢!

    Hibernate3+中文参考文档

    19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One ...

    hibernate3.04中文文档.chm

    20.5. 理解集合性能(Understanding Collection performance) 20.5.1. 分类(Taxonomy) 20.5.2. Lists, maps 和sets用于更新效率最高 20.5.3. Bag和list是反向集合类中效率最高的 20.5.4. 一次性删除(One...

Global site tag (gtag.js) - Google Analytics