问题
I have following configuration file -
<hibernate-configuration>
<session-factory name="HibernateSessionFactory">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">mysql</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="com/kshitiz/Game.hbm.xml"/>
</session-factory>
</hibernate-configuration>
This is my code for building a session factory -
public static void main(String[] args) {
Configuration config = new Configuration().configure("hibernate.cfg.xml");
sf = config.buildSessionFactory();
}
I am getting this exception -
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:75)
at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:113)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:378)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
at com.kshitiz.Main.main(Main.java:11)
Edit:
<hibernate-mapping>
<class name="com.kshitiz.Game" table="GAME">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="date" type="java.util.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>
My question - Why is Hibernate trying to perform a JNDI lookup when I haven't configured anything of that sort in my project?
回答1:
It is not actually failing because of lookup, but because it tries to make binding to JNDI. It does so because here:
<session-factory name="HibernateSessionFactory">
you give name of session factory Hibernate tries to bind session factory to JNDI. Most likely you do not have JNDI configured for your Java SE application, so name attribute is not needed. Just omit it:
<session-factory>
回答2:
I guess the problem is in hibernate.cfg.xml file.
Try with this configuration.
<session-factory name="java:/hibernate/SessionFactory">
<property name="session_factory_name">java:hibernate/SessionFactory</property>
<!-- Add your configurations -->
</session-factory>
回答3:
at com.kshitiz.Main.main(Main.java:11)
I guess the problem is here....
Can you have relook on whether there is any java file without class names? or that is not mapped in your Game.hbm.xml
?
It is not that Hibernate will perform a JNDI lookup always. It happens only when specified.
来源:https://stackoverflow.com/questions/11625131/why-does-hibernate-perform-a-jndi-lookup