本文主要是介绍4.Hibernate首例:插入数据save,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package com.bjpowernode.hibernate;import java.util.Date;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Client {public static void main(String[] args) {//读取hibernate.cfg.xml文件Configuration cfg = new Configuration().configure();//建立SessionFactorySessionFactory factory = cfg.buildSessionFactory();//取得sessionSession session = null;try {session = factory.openSession();//开启事务session.beginTransaction();User user = new User();user.setName("张三");user.setPassword("123");user.setCreateTime(new Date());user.setExpireTime(new Date());//保存User对象session.save(user);//提交事务session.getTransaction().commit();}catch(Exception e) {e.printStackTrace();//回滚事务session.getTransaction().rollback();}finally {if (session != null) {if (session.isOpen()) {//关闭sessionsession.close();}}}}
}
3.Hibernate首例:把hbm生成ddl
这篇关于4.Hibernate首例:插入数据save的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!