0

データベース内の 2 つの異なるテーブルにアクセスしようとしています。

最初に私のpersistence.xmlは次のとおりでした:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="Persistence">
        <jta-data-source>jdbc/classicmodels</jta-data-source>
        <class>com.tugay.maythirty.model.Customers</class>
        <class>com.tugay.maythirty.model.Products</class>
        <class>com.tugay.maythirty.model.Employee</class>
        <class>com.tugay.maythirty.model.Office</class>
    </persistence-unit>
</persistence>

だから私はこの素晴らしい記事に従ってGlassfishでDataSourceを定義し、私のアプリケーションは正常に動作していました. name="Persistence"の DAO クラスで@PersistenceContextアノテーションを使用していました

これは基本的に「classicmodels」というテーブルに接続されています。

次に、「sakila」というテーブルからデータを取得する必要がありました。次の行を persistence.xml に追加しました。

<persistence-unit name="sakila">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.tugay.maythirty.model.Actor</class>
    <class>com.tugay.maythirty.model.Film</class>
    <class>com.tugay.maythirty.model.FilmActor</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/sakila"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="password"/>
    </properties>
</persistence-unit>

そして、DAOでこのコードを使用しました:

public List<Actor> getAllActors(){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("sakila");
        EntityManager em = emf.createEntityManager();
        TypedQuery<Actor> actorTypedQuery = em.createQuery("Select a from Actor a",Actor.class);
        return actorTypedQuery.getResultList();
}

アプリケーションをGlassFishにデプロイすると、次の例外が発生し始めました:

java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [Persistence] in the scope of the module called [may-thrity]. Please verify your application.

そのため、「Persistence」という名前の永続ユニットがなくなったようです。

私が間違っているのは何ですか?

4

1 に答える 1

0

永続化ユニットを明示的に宣言しようとしましたか

@PersistenceContext(unitName="Persistence")

それ以外の

@PersistenceContext(name="Persistence")

PersistenceContext ドキュメント

于 2013-06-17T23:51:08.947 に答える