|
|
Problems with JPA: locating peristence.xml
I can't seem to figure out exactly what to do here. I keep receiving the following error when attempting to inject into my DAO class:
Context initialization failed
org..beans.factory.BeanCreationExce ption: Error creating bean with name 'Dao' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org..beans.factory.NoSuchBeanDefini tionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: No EntityManagerFactory found for persistence unit name 'AppPU'
Caused by: org..beans.factory.NoSuchBeanDefini tionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: No EntityManagerFactory found for persistence unit name 'AppPU' at org..orm.jpa.support.PersistenceAnn otationBeanPostProcessor.findNamedEntityManagerFac tory(PersistenceAnnotationBeanPostProcessor.java:1 72) at org..orm.jpa.support.PersistenceAnn otationBeanPostProcessor.findEntityManagerFactory( PersistenceAnnotationBeanPostProcessor.java:149) at org..orm.jpa.support.PersistenceAnn otationBeanPostProcessor$AnnotatedMember.resolve(P ersistenceAnnotationBeanPostProcessor.java:270) at org..orm.jpa.support.PersistenceAnn otationBeanPostProcessor$AnnotatedMember.inject(Pe rsistenceAnnotationBeanPostProcessor.java:217) at org..orm.jpa.support.PersistenceAnn otationBeanPostProcessor.postProcessAfterInstantia tion(PersistenceAnnotationBeanPostProcessor.java:9 1) at org..beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:402) at org..beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 45)
...
...
My app's structure is :
customer.war --gt; META-INF/MANIFEST.MF --gt; META-INF/persistence.xml --gt; WEB-INF/classes --gt; WEB-INF/classes/data/DaoJpaImpl.class --gt; WEB-INF/classes/domain/Customer.class --gt; WEB-INF/lib --gt; WEB-INF/web.xml --gt; WEB-INF/applicationContext.xml
...
My persistence.xml file:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;persistence version=quot;1.0quot; xmlns=quot;xml/ns/persistencequot; xmlns:xsi=quot;2001/XMLSchema-instancequot; xsi:schemaLocation=quot;xml/ns/persistence xml/ns/persistence/persistence_1_0.xsdquot;gt; lt;persistence-unit name=quot;AppPUquot; transaction-type=quot;JTAquot;gt; lt;jta-data-sourcegt;enrollmentlt;/jta-data-sourcegt; lt;classgt;domain.Customerlt;/classgt; lt;properties/gt; lt;/persistence-unitgt;
lt;/persistencegt;
My applicationContext file:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;beans xmlns=quot;schema/beansquot; xmlns:xsi=quot;2001/XMLSchema-instancequot; xmlns:aop=quot;schema/aopquot; xmlns:tx=quot;schema/txquot; xsi:schemaLocation=quot; schema/beans schem...-beans-2.0.xsd schema/tx schem...ing-tx-2.0.xsd schema/aop schema/aop/spring-aop-2.0.xsdquot;gt; lt;!-- JPA annotations bean post processor --gt; lt;bean class=quot;org..orm.jpa.support.Persist enceAnnotationBeanPostProcessorquot;/gt; lt;bean id=quot;Daoquot; class=quot;data.DaoJpaImplquot;/gt;
lt;/beansgt;
My DAO class:
public class DaoJpaImpl { @Resource private UserTransaction utx; @PersistenceUnit(unitName = quot;AppPUquot;) private EntityManagerFactory entityManagerFactory; private EntityManager getEntityManager() { return this.entityManagerFactory.createEntityManager(); }
...
...
And finally my Customer domain object:
@Entity
@Table(name = quot;CUSTOMERquot;)
@NamedQueries( {@NamedQuery(name = quot;Customer.findByCustomerIdquot;, query = quot;SELECT c FROM Customer c WHERE c.customerId = :customerIdquot;), @NamedQuery(name = quot;Customer.findByNamequot;, query = quot;SELECT c FROM Customer c WHERE c.name = :namequot;)})
public class Customer implements Serializable {
@Id @Column(name = quot;CUSTOMER_IDquot;, nullable = false) private Integer customerId;
@Column(name = quot;NAMEquot;) private String name;
...
...
*********************
I'm using Netbeans. By default, it adds persistence.xml to classes/META-INF/persistence.xml. I tried moving it to where it is now by modifying the build so it goes to META-INF/persistence.xml. Same error though.
Where is Spring expecting this file to be? The problem seems to be that Spring is looking somewhere else for this file. I'm using Spring 2.0 RC4.
Any help would be appreciated.
Thanks,
Mike
Can someone verify for me that Spring should look in one of two places for persistence.xml (when using persistence.xml file for JPA configuration vs. specifying in a Spring config file):
WEB-INF/META-INF
-or-
WEB-INF/classes/META-INF
I know there have been some recent changes around how Spring looks for this file but I'm under the impression that those issues have been resolved with RC4.
Thanks,
Mike
If it's part of your war file then WEB-INF/classes/META-INF is the location to use. Also, I don't see where you configure the EntityManagerFactory in your configuration file. I think that's your problem, not the location of persistence.xml. |
|