问题描述
- 初学hibernate,Configuration().configure()错误
-
初学hibernate,全部都是自己手写的,配置文件时参考hibernateAPI写的,但每次运行到
conf = new Configuration().configure();时就会直接跳到finally了。请问是我的配置文件写错了还是哪里出错了呢???
配置文件:
<?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"><session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1:3306/lcfer</property> <property name="connection.username">root</property> <property name="connection.password"></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 使Hibernate的自动会话上下文管理--> <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">create</property> <mapping resource="com/model/User.hbm.xml"/> </session-factory>
测试类:
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;import com.model.User;
public class UserTest {
@Test
public void test(){
User u = new User();
u.setUserid(6);
u.setName("lin");
u.setAccount("L");
u.setPassword("123");
u.setSix(true);
u.setUsergroup("管理员");Configuration conf = null; SessionFactory sessionFactory = null; Session session = null; Transaction tx = null; try { conf = new Configuration().configure(); sessionFactory = conf.buildSessionFactory(); session = sessionFactory.openSession(); tx = session.beginTransaction(); session.save(u); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); } finally{ session.close(); sessionFactory.close(); } }
}
解决方案
你把报的错贴出来看下啊.
时间: 2024-09-19 19:57:17