hibernate配置hibernate-cfg.xml配置中在sessionFactory加不加名字的区别

<!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 name="foo">
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql:///hibernate</property>
		<property name="connection.username">root</property>
		<property name="connection.password">root</property>
		<property name="hbm2ddl.auto">create</property>
		<property name="show_sql">true</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<mapping resource="cn/hibernate/model/SysUser.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

 注意:加上name会引起下面的异常

警告: Could not bind factory to JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
	at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
	at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
	at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
	at javax.naming.InitialContext.getNameParser(InitialContext.java:480)
	at org.hibernate.util.NamingHelper.bind(NamingHelper.java:52)
	at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90)
	at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:306)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
	at cn.hibernate.Utils.HibernateUtils.<clinit>(HibernateUtils.java:13)
	at cn.hibernate.dao.SysUserDao.<init>(SysUserDao.java:10)
	at cn.hibernate.text.UserTest.main(UserTest.java:15)
2014-3-7 13:50:29 org.hibernate.tool.hbm2ddl.SchemaExport execute
信息: Running hbm2ddl schema export

 解决办法就是去掉name=“foo”属性,因为有了name属性hibernate会试图把这个sessionfacotry注册到jndi中去。

你可能感兴趣的:(sessionFactory)