亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JndiException:在獨立應用程序中解析 JNDI 名稱時出錯

JndiException:在獨立應用程序中解析 JNDI 名稱時出錯

www說 2023-08-16 16:14:30
我正在嘗試在沒有任何應用程序服務器的獨立應用程序中使用 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>
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

好像不能用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() {

    if (sessionFactory == null) {

        try {

            // Create registry

            registry = new StandardServiceRegistryBuilder().configure().build();


            // Create MetadataSources

            MetadataSources sources = new MetadataSources(registry);


            // Create Metadata

            Metadata metadata = sources.getMetadataBuilder().build();


            // Create SessionFactory

            sessionFactory = metadata.getSessionFactoryBuilder().build();


        } catch (Exception e) {

            e.printStackTrace();

            if (registry != null) {

                StandardServiceRegistryBuilder.destroy(registry);

            }

        }

    }

    return sessionFactory;

}


public static void shutdown() {

    if (registry != null) {

        StandardServiceRegistryBuilder.destroy(registry);

    }

}

它現在可以工作了,但我想我可以使用 EntityManager?!


查看完整回答
反對 回復 2023-08-16
  • 1 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號