gpt4 book ai didi

java - 为什么 Hibernate 执行 JNDI 查找?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:59:13 27 4
gpt4 key购买 nike

我有以下配置文件 -

<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>

这是我构建 session 工厂的代码 -

public static void main(String[] args) {
Configuration config = new Configuration().configure("hibernate.cfg.xml");
sf = config.buildSessionFactory();
}

我收到这个异常 -

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)

编辑:

<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>

我的问题 - 当我没有在我的项目中配置任何此类内容时,为什么 Hibernate 会尝试执行 JNDI 查找?

最佳答案

它实际上并不是因为查找而失败,而是因为它试图绑定(bind)到 JNDI。这样做是因为这里:

<session-factory name="HibernateSessionFactory">

您提供 session 工厂的名称 Hibernate 尝试将 session 工厂绑定(bind)到 JNDI。您很可能没有为 Java SE 应用程序配置 JNDI,因此不需要名称属性。只是省略它:

<session-factory>

关于java - 为什么 Hibernate 执行 JNDI 查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11625131/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com