3

Glass Fish サーバーで休止状態と JPA 2.0 を使用して、新しい Java EE プロジェクトを作成しようとしています。シームレスに動作するように上記を構成するためのリソースをいくつか提供してもらえますか? netbeans を使用して、休止状態プロバイダーを使用して永続化ユニットを生成しようとしましたが、最終的に次のエラーが発生します。

javax.persistence.PersistenceException: [PersistenceUnit: DBAppPU] Unable to build EntityManagerFactory
4

2 に答える 2

6

まず、更新ツールを介してHibernateサポートをインストールします(または手動の手順に従います)。次に、persistence.xmlJPAプロバイダーとしてHibernateを使用するためのJPA2.0を提供します。

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
  <persistence-unit name="MyPu" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <!-- JNDI name of the database resource to use -->
    <jta-data-source>jdbc/__default</jta-data-source>
    <properties>
      <!-- The database dialect to use -->
      <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
      <!-- update database tables at deployment -->
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <!-- log the generated SQL -->
      <property name="hibernate.show_sql" value="true"/>
    </properties>
  </persistence-unit>
</persistence>

資力

于 2010-08-31T16:58:54.200 に答える
0

このガイダンスは、hibernate.4.3.5 と EJB および GlassFish.4.0 を NetBeans.8.0 IDE に統合するためのものです。ネット Bean で Web プロジェクトを作成し、プロジェクトに hibernate jar ファイルを追加します。MySql と glassfish の構成に関連するその他の設定は非常に簡単なので、この記事では説明しません。次に、次のように persistence.xml ファイルを作成します。

<persistence-unit name="omidashouriPU" transaction-type="Resource_Local">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/YourSchemaName"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="yourpassword"/>
            <property name="hibernate.show_sql" value="true"/>
    </properties>
</persistence-unit>

EntityManager を作成するための EJB クラス (@Stateless でアノテーションを付けたクラス) では、次の構文を使用します。

EntityManagerFactory emf = Persistence.createEntityManagerFactory("omidashouriPU");
EntityManager em = emf.createEntityManager();
em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(YourEntityObject);
em.getTransaction().end();

As you Know when you are using “transaction-type="Resource_Local", you have to manage the transaction by yourself, mean that, managing of opening and closing the transaction is our responsibility.
于 2014-07-13T09:12:32.517 に答える