Test.java
import org.hibernate.cfg.*;
import org.hibernate.*;
public class Test {
public static void main(String[] args) throws Exception {
Student student = new Student();
student.setName("Jack");
student.set...;// 设置student 对象
Configuration config = new Configuration();// 1. Configuration类,读配置文件和映射文件
//read hibernate.cfg.xml and hbm.xml
config = config.configure();
SessionFactory sf = config.buildSessionFactory();// 2. SessionFactory 工厂类 接口
Session session = sf.openSession();// 3. Session 核心
Transaction trans = session.beginTransaction();
session.save(student);
//User user = session.load(User.class,new Long(1001));
//session.update(user);
//session.delete(user);
trans.commit();
session.close();
}
}
本文介绍了一个简单的Hibernate使用案例,展示了如何通过Java代码实现学生信息的保存操作。具体步骤包括:创建Configuration实例并加载配置文件、建立SessionFactory、开启Session、启动事务、保存对象以及提交事务。
221

被折叠的 条评论
为什么被折叠?



