猿问

JndiException:在独立应用程序中解析 JNDI 名称时出错

我正在尝试在没有任何应用程序服务器的独立应用程序中使用 hibernate 和 h2 数据库。


另外,我可以通过一些数据库工具连接到 h2 数据库(使用文件模式进行测试)。


我已将我的项目设置为 Maven 项目,包括以下依赖项:


<dependency>

        <groupId>com.h2database</groupId>

        <artifactId>h2</artifactId>

        <version>1.4.199</version>

</dependency>


<dependency>

    <groupId>org.hibernate</groupId>

    <artifactId>hibernate-entitymanager</artifactId>

    <version>5.4.4.Final</version>

</dependency>


<dependency>

    <groupId>org.hibernate</groupId>

    <artifactId>hibernate-core</artifactId>

    <version>5.4.4.Final</version>

</dependency>


<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>5.1.9.RELEASE</version>

</dependency>

persistence.xml(资源/META-INF/persistence.xml)


<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">


    <persistence-unit name="sow-quest-unit">

        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <!--        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>-->

        <!--        <jta-data-source>jdbc:h2:file:D:\Projects\SecretWoods\db</jta-data-source>-->

        </properties>

    </persistence-unit>


</persistence>


www说
浏览 135回答 1
1回答

蝴蝶不菲

好像不能用EntityManager吧?!我现在尝试以下操作:删除persitence.xml,编辑hibernate.cfg.xml(添加以下内容):<property name="current_session_context_class">thread</property><property name="hibernate.dbcp.initialSize">5</property><property name="hibernate.dbcp.maxTotal">20</property><property name="hibernate.dbcp.maxIdle">10</property><property name="hibernate.dbcp.minIdle">5</property><property name="hibernate.dbcp.maxWaitMillis">-1</property><mapping class="com.wolfo.quest.Quest" /><mapping class="com.wolfo.quest.QAnswer" /><mapping class="com.wolfo.quest.QQuestion" /><mapping class="com.wolfo.quest.QText" />之后我写了一些课程(在示例中找到):private static StandardServiceRegistry registry;private static SessionFactory sessionFactory;public static SessionFactory getSessionFactory() {&nbsp; &nbsp; if (sessionFactory == null) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create registry&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; registry = new StandardServiceRegistryBuilder().configure().build();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create MetadataSources&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MetadataSources sources = new MetadataSources(registry);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create Metadata&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Metadata metadata = sources.getMetadataBuilder().build();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create SessionFactory&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sessionFactory = metadata.getSessionFactoryBuilder().build();&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (registry != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StandardServiceRegistryBuilder.destroy(registry);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return sessionFactory;}public static void shutdown() {&nbsp; &nbsp; if (registry != null) {&nbsp; &nbsp; &nbsp; &nbsp; StandardServiceRegistryBuilder.destroy(registry);&nbsp; &nbsp; }}它现在可以工作了,但我想我可以使用 EntityManager?!
随时随地看视频慕课网APP

相关分类

Java
我要回答