当前位置: 代码迷 >> Sql Server >> 刚刚用hibernate就报错了 Configured SessionFactory: null求大神帮帮忙
  详细解决方案

刚刚用hibernate就报错了 Configured SessionFactory: null求大神帮帮忙

热度:71   发布时间:2016-04-24 10:27:42.0
刚用hibernate就报错了 Configured SessionFactory: null求大神帮帮忙
我的java文件:

public class testFrist_1 {

@Test
public void test() {
Configuration cfg= new Configuration().configure();

ServiceRegistry serviceRegistry=new ServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build();

SessionFactory factory=cfg.buildSessionFactory(serviceRegistry);
Session session=factory.openSession();

//开启事务
User u=new User();
u.setUsername("zhangsan");
u.setNickname("xiaosan");
u.setPassword("111");
u.setBorn(new Date());

//提交事务
session.getTransaction().commit();
}

}

我的**.hbm.xml文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
        

<hibernate-mapping package="com.test.hibernate">
<class name="user" table="t_user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="username"></property>
<property name="password"></property>
<property name="nickname"/>
<property name="born" type="calendar_date"/>
</class>
</hibernate-mapping>

我的hibernate.cfg.xml文件
<hibernate-configuration>
<session-factory >
<!-- 数据库连接 -->
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://127.0.0.1:1433;DatabaseName=hibernate</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">986681543</property>

<!-- 在控制台显示sql语句 -->
<property name="show_sql">true</property>
<!-- 更具需要自动生成和更新数据库 -->
<property name="hibernate.hbm2ddl.auto">update</property>

<!-- 加入实体类的配置文件 -->
<mapping   resource="com/hibernate/test/User.hbm.xml"/>

</session-factory>
</hibernate-configuration>


结果就报错了  下面红色的是什么原因
三月 26, 2014 10:51:18 上午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
三月 26, 2014 10:51:18 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.4.Final}
三月 26, 2014 10:51:18 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
三月 26, 2014 10:51:18 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
三月 26, 2014 10:51:18 上午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
三月 26, 2014 10:51:18 上午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
三月 26, 2014 10:51:18 上午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/hibernate/test/User.hbm.xml
三月 26, 2014 10:51:18 上午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
三月 26, 2014 10:51:18 上午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)

------解决方案--------------------
LZ确定是new ServiceRegistryBuilder().applySettings(cfg.getProperties()).build()而不是new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry()?
  相关解决方案