hibernate demo

来源:互联网 发布:相片变成漫画图软件 编辑:程序博客网 时间:2024/06/01 17:58

创建一个项目

引入hibernatejar包,mysql链接jar包

配置hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <!-- Database connection settings -->        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>        <property name="connection.username">root</property>        <property name="connection.password">111111</property>        <!-- JDBC connection pool (use the built-in) -->        <property name="connection.pool_size">1</property>        <!-- SQL dialect -->        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>        <!-- Enable Hibernate's automatic session context management -->        <property name="current_session_context_class">thread</property>        <!-- Disable the second-level cache  -->        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>        <!-- Echo all executed SQL to stdout -->        <property name="show_sql">true</property>        <!-- Drop and re-create the database schema on startup -->        <property name="hbm2ddl.auto">update</property>       <!--   <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>-->       <mapping class="hibernate.bean.Student" />    </session-factory></hibernate-configuration>

从文档上copy的

接着实体类

@Entitypublic class Student {@Id@GeneratedValueprivate int id;private String name;private String password;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}

工具类

package hibrenate.util;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.cfg.Configuration;public class HibernateUtil {    private static final SessionFactory sessionFactory = buildSessionFactory();    private static SessionFactory buildSessionFactory() {        try {            // Create the SessionFactory from hibernate.cfg.xml            return new Configuration().configure().buildSessionFactory();        }        catch (Throwable ex) {            // Make sure you log the exception, as it might be swallowed            System.err.println("Initial SessionFactory creation failed." + ex);            throw new ExceptionInInitializerError(ex);        }    }    public static SessionFactory getSessionFactory() {        return sessionFactory;    }}

然后测试save
public void testSave(){SessionFactory sessionFactory=HibernateUtil.getSessionFactory();Session session=sessionFactory.getCurrentSession();Student s = new Student();s.setName("zt1");s.setPassword("111111");Transaction t = session.beginTransaction();session.save(s);s.setName("zt3");t.commit();}
目的是想看看在事务提交前改字段有没有效

可是一运行报错,说找不到hibernate.cfg.xml

原因写成hiibernate.cfg.xml

再运行还是出错

An AnnotationConfiguration instance is required to use <mapping class="hibernate.bean.Student"/>

原因:是注释方式,所以在HibernateUtil类里Configuration要改为AnnotationConfiguration

测试update

public void testUpdate(){SessionFactory sessionFactory=HibernateUtil.getSessionFactory();Session session=sessionFactory.getCurrentSession();Student s = (Student) session.load(Student.class, 1);s.setName("99999");s.setPassword("2222222");Transaction t = session.beginTransaction();session.update(s);s.setName("asdashdsalkj");t.commit();}
run出错

load is not valid without active transaction

解决方法

是先事务开启,再执行load方法

public void testUpdate(){SessionFactory sessionFactory=HibernateUtil.getSessionFactory();Session session=sessionFactory.getCurrentSession();Transaction t = session.beginTransaction();Student s = (Student) session.load(Student.class, 1);s.setName("99999");s.setPassword("2222222");session.update(s);s.setName("asdashdsalkj");t.commit();}
解决

0 0
原创粉丝点击