このガイダンスは、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.