hibernate3学习笔记(四) 增删改查[Java编程]
本文“hibernate3学习笔记(四) 增删改查[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
测试代码:
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学习笔记(四) 增删改查[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |