快速业务通道

hibernate3学习笔记(四) 增删改查 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-20

hibernate3学习笔记(四) 增删改查

时间:2011-02-01

测试代码:

package com.hb3.pack_01;

import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration;

import com.hb3.pack_01.model.User;

public class BusinessService {

public static void main(String[] args) { Configuration config = new Configuration().configure(); SessionFactory sessionFactory = config.buildSessionFactory(); Session session = sessionFactory.openSession(); //insert User user = new User(); user.setName("chenyan"); //user.setAge(new Integer(23)); Transaction tx = session.beginTransaction(); session.save(user); tx.commit(); //get user = (User) session.get(User.class, user.getId()); System.out.println(user.getAge()); //update user.setAge(new Integer(24)); tx= session.beginTransaction(); session.update(user); tx.commit();

//load user = (User) session.load(User.class, user.getId()); System.out.println(user.getAge()); //delete tx = session.beginTransaction(); session.delete(user); tx.commit(); session.close(); sessionFactory.close(); } } 执行结果:

11:50:50,750 WARN ConfigurationFactory:127 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/Java/MyEclipse%206.0/workspace/hb3demo/ehcache-1.2.3.jar!/ehcache-failsafe.xml 11:50:51,156 WARN EhCacheProvider:93 - Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults. 11:50:51,171 WARN EhCacheProvider:93 - Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults. Hibernate: insert into user (name, age) values (?, ?) null Hibernate: update user set name=?, age=? where id=? 24

hibernate3学习笔记(四) 增删改查(2)

时间:2011-02-01

修改User.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">

<hibernate-mapping> <class name="com.hb3.pack_01.model.User" table="user" dynamic-insert="true" dynamic-update="true" > <id name="id" column="id" type="java.lang.Integer"> <generator class="native" /> </id> <property name="name" column="name" type="java.lang.String" /> <property name="age" column="age" type="java.lang.Integer" /> </class> </hibernate-mapping> 运行结果如下:

12:46:32,062 WARN ConfigurationFactory:127 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/Java/MyEclipse%206.0/workspace/hb3demo/ehcache-1.2.3.jar!/ehcache-failsafe.xml 12:46:32,468 WARN EhCacheProvider:93 - Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults. 12:46:32,500 W

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号