我试图访问我的数据库中的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中定义了一个数据源,我的应用程序运行良好。我在DAO类中使用@PersistenceContext注释,name="Persistence"
这基本上连接到一个名为"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"的持久性单元不见了。
我做错了什么?
您是否尝试过用
显式地声明您的持久性单元? @PersistenceContext(unitName="Persistence")
代替
@PersistenceContext(name="Persistence")
PersistenceContext医生