hibernate 報錯,在session.save(s)出錯,求教
根據(jù)視頻來的,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(),"武當(dāng)山");
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&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
你用的是Hibernate5.0及以上版本,不需要ServiceRegistry了,視頻老師用的是4.X版本的Hibernate。
將代碼改為下面的就可以了
@Before
?? ?public void init()
?? ?{
?? ??? ?/*
?? ??? ? * hibernate 5.0以上版本,不通過ServiceRegistry
?? ??? ? */
?? ??? ?//創(chuàng)建配置對象
?? ??? ?Configuration configuration=new Configuration().configure();
?? ??? ?//創(chuàng)建會話工廠對象
?? ??? ?sessionFactory = configuration.buildSessionFactory();
?? ??? ?//創(chuàng)建會話對象
?? ??? ?session=sessionFactory.openSession();
?? ??? ?//開啟事務(wù)
?? ??? ?transation= session.beginTransaction();
?? ?}
如果還有其他問題,請看http://bbs.csdn.net/topics/391955731
我也是出現(xiàn)這個問題,后來在評論區(qū)找到的答案,框架學(xué)起來有點吃力?。。。?br />
2017-12-27
遇到相同問題,已解決
http://bbs.csdn.net/topics/391955731