Question

I am trying to access 2 different tables in my database.

Initially my persistence.xml was as follows:

<?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>

So I defined a DataSource in Glassfish following this great artical and my application was working fine. I was using @PersistenceContext annotation in my DAO classes with name="Persistence"

This basically is connected to a table called "classicmodels".

Then I needed to fetch some data from a table called "sakila". I added these lines to my 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>

And I have used this code in my 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();
}

When I deploy my application to GlassFish however I started getting this Exception:

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.

So it seems that now my Persistence Unit with name "Persistence" is gone.

What is it that I am doing wrong?

Was it helpful?

Solution

Have you tried explicitly declaring your persistence unit with

@PersistenceContext(unitName="Persistence")

instead of

@PersistenceContext(name="Persistence")

PersistenceContext doc

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top