package cn.reghao.tnb.sb.db.hibernate;

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

/**
 * @author reghao
 * @date 2023-04-13 15:50:31
 */
public class HibernateDemo {
    public static void main(String[] args) {
        User user = new User("zhangsan", 1);

        Configuration configuration = new Configuration();
        configuration.configure("/hibernate.cfg.xml")
                .addAnnotatedClass(User.class);

        SessionFactory sessionFactory = configuration.buildSessionFactory();
        // 打开Session
        Session session = sessionFactory.openSession();
        // 开始一个事务
        Transaction trans = session.beginTransaction();
        // 持久化操作
        session.save(user);
        // 提交事务
        trans.commit();
        // 关闭Session
    }
}
