package com.study.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import com.study.dao.po.Student;
import com.study.dao.po.Teacher;
public class TeacherTest {
public static void main (String[] agr0){
//Configuration cfg = new Configuration().configure("/hibernate.cfg.xml");
//SessionFactory sf = cfg.buildSessionFactory();
// Session session = sf.openSession();
// Transaction t = session.beginTransaction();
// Student stu = new Student();
// stu.setId(1002);
// stu.setName("wangwu");
// stu.setAge(20);
// session.save(stu);
// t.commit();
// session.close();
//Configuration cfg = new Configuration().configure()
Configuration cfg = new AnnotationConfiguration().configure();
//cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
StandardServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
SessionFactory sf = cfg.buildSessionFactory(sr);
// SessionFactory sf =cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Teacher t = new Teacher();
t.setId(1004);
t.setName("wangwu11");
t.setTitle("教授");
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
}
}
阅读(837) | 评论(0) | 转发(0) |