问答详情
源自:1-8 创建持久化类

hibernate 报错,在session.save(s)出错,求教

根据视频来的,hibernate用的hibernate-release-5.2.5.Final

我的程序日志,然后就停了,请问是什么错误。

十二月 09, 2016 10:03:32 上午 org.hibernate.Version logVersion

INFO: HHH000412: Hibernate Core {5.2.5.Final}

十二月 09, 2016 10:03:32 上午 org.hibernate.cfg.Environment <clinit>

INFO: HHH000206: hibernate.properties not found

十二月 09, 2016 10:03:32 上午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>

INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}

十二月 09, 2016 10:03:32 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)

十二月 09, 2016 10:03:32 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql:mem:hibernate;DB_CLOSE_DELAY=-1;MVCC=TRUE]

十二月 09, 2016 10:03:32 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH10001001: Connection properties: {user=root, password=****}

十二月 09, 2016 10:03:32 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH10001003: Autocommit mode: false

十二月 09, 2016 10:03:32 上午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>

INFO: HHH000115: Hibernate connection pool size: 20 (min=1)


源码:

import java.util.Date;


import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import org.hibernate.cfg.Configuration;

import org.hibernate.service.ServiceRegistry;

import org.junit.After;

import org.junit.AfterClass;

import org.junit.Before;

import org.junit.BeforeClass;

import org.junit.Test;


public class StudentTest {

private SessionFactory sessionFactory;

private Session session;

private Transaction transaction;

@Before

public void InitialContext(){

Configuration config = new Configuration().configure();

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();

sessionFactory = config.buildSessionFactory(serviceRegistry);

session = sessionFactory.openSession();

transaction = session.beginTransaction();

}

@After

public void destory(){

transaction.commit();

session.close();

sessionFactory.close();

}

@Test

public void testSaveStudents(){

Student s = new Student(1,"张三丰","男",new Date(),"武当山");

session.save(s);

}


}

hibernate配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

    <property name="connection.username">root</property>

    <property name="connection.password">1234</property>

    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

    <!-- 

    <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>

    -->

    <property name="connection.url">jdbc:mysql:mem:hibernate;DB_CLOSE_DELAY=-1;MVCC=TRUE</property>

     

    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

   

    <property name="show_sql">true</property>

    <property name="format_sql">true</property>

    <property name="hbm2ddl.auto">create</property>

   

    <mapping resource="Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

在session.save(s)出错,求教


提问者:人在梦游中 2016-12-09 10:14

个回答

  • uestc_chen
    2016-12-09 11:55:23
    已采纳

    你用的是Hibernate5.0及以上版本,不需要ServiceRegistry了,视频老师用的是4.X版本的Hibernate。

    将代码改为下面的就可以了

    @Before
        public void init()
        {
            /*
             * hibernate 5.0以上版本,不通过ServiceRegistry
             */
            //创建配置对象
            Configuration configuration=new Configuration().configure();
            //创建会话工厂对象
            sessionFactory = configuration.buildSessionFactory();
            //创建会话对象
            session=sessionFactory.openSession();
            //开启事务
            transation= session.beginTransaction();
        }

    如果还有其他问题,请看http://bbs.csdn.net/topics/391955731

    我也是出现这个问题,后来在评论区找到的答案,框架学起来有点吃力啊!!!

  • 阿西莫夫
    2017-12-27 22:11:37

    遇到相同问题,已解决

    http://bbs.csdn.net/topics/391955731