session.getTransaction()只是根据session获得一个Transaction实例,但是并没有启动它
session.beginTransaction()在获得一个Transaction后调用其begin()方法
(1)
session = HibernateUtil.getSession();//这是开启Session的操作
session.beginTransaction();//这是开启Transaction的操作
session.save(user);//这正是把数据放入一级缓存session中的操作
session.getTransaction().commit();//此处才是真正与数据库交互的语句
(2)
Session session=HibernateTools.getSession();
Transaction tx=session.beginTransaction();
session.save(user);
tx.commit();
session.close();
如:
public static void main(String[] args) throws ParseException {
//获取配置文件
Configuration configuration = new Configuration().configure();
//注册服务对象
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
StandardServiceRegistry serviceRegistry = serviceRegistryBuilder.applySettings(configuration.getProperties()).build();
//获取SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//获取session
Session session = sessionFactory.openSession();
//开启事务
session.beginTransaction();
//************开始操作*************//
//1. 利用 get 方法获取 id 为 1 的用户数据
Commodity getCommodity = (Commodity) session.get(Commodity.class, 1);
System.out.println("创建日期:" + getCommodity.getCreateDate() + "描述:" + getCommodity.getDescribe() + "修改日期:" + getCommodity.getModifyDate() + "名称:" + getCommodity.getName());
// Date date = new Date(0);
String stringdate = "2019-05-07";
Date date = new SimpleDateFormat("YYYY-MM-DD").parse(stringdate);
//2.增加商品
// Commodity newCommodity = new Commodity(date,"电器1",date,"电饭锅2");
// session.save(newCommodity);
//3.删
// Commodity getCommodity1 = (Commodity) session.load(Commodity.class, 2);
// session.delete(getCommodity1);
// session.getTransaction().commit();
//4.改
// Commodity getCommodity2 = (Commodity) session.get(Commodity.class, 1);
// getCommodity2.setName("洗脸盆");
// session.update(getCommodity2);
// session.getTransaction().commit();
//************结束操作*************//
//提交事务
session.getTransaction();
//关闭session
session.close();
//关闭SessionFactory
sessionFactory.close();
}
再如:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import online.shixun.shop.model.User;
public class HibernateDemo {
public static void main(String[] args) {
// 获取配置文件
Configuration configuration = new Configuration().configure();
// 注册服务对象
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
StandardServiceRegistry serviceRegistry = serviceRegistryBuilder.applySettings(configuration.getProperties()).build();
// 获取 SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
// 获取 Session
Session session = sessionFactory.openSession();
// 开启事务
session.beginTransaction();
//***************开始数据操作***************//
//1. 利用 get 方法获取 id 为 1 的用户数据
User getUser = (User) session.get(User.class, 1);
System.out.println("Get 用户名/密码/年龄:" + getUser.getName() + "/" + getUser.getPassword() + "/" + getUser.getAge());
//2. 利用 load 方法获取 id 为 1 的用户数据
User loadUser = (User) session.load(User.class, 1);
System.out.println("Load 用户名/密码/年龄:" + loadUser.getName() + "/" + loadUser.getPassword() + "/" + loadUser.getAge());
//3. 利用 save 方法新增用户数据(执行完成以下语句,利用数据库客户端工具查询表中是否有新数据增加)
User newUser = new User("武汉", "innovaee", 16);
session.save(newUser);
//4. 利用 saveOrUpdate 方法,修改 getUser 数据
getUser.setName("轻实训!");
session.saveOrUpdate(getUser);
//5. 利用 delete 方法删除 getUser 数据
session.delete(getUser);
//***************结束数据操作***************//
// 提交事务
session.getTransaction().commit();
// 关闭 Sessin
session.close();
// 关闭 SessionFactory
sessionFactory.close();
}
}