Seguinte estou estudando o livro spring in Action fiz os teste usando
HibernateTemplate e funcionou tudo ok. Ai fui tentar fazer uns
exemplos usando JpaTemplate, ai começou meu problema.
Minha configuração esta assim
web.xml
<!-- Spring ApplicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springContext*.xml</param-value>
</context-param>
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-
class>
</listener>
===============================================================
ApplicationContext.xml
O nome deste arquivo é springContextDB.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the
DAOs in
- dataAccessContext-local/jta.xml (see web.xml's
"contextConfigLocation").
-->
<beans xmlns="
http://www.springframework.org/schema/beans"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="
http://www.springframework.org/schema/aop"
xmlns:tx="
http://www.springframework.org/schema/tx"
xmlns:jee="
http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/banco"
resource-ref="true"/>
<bean id="jpaTemplate"
class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory" ref="entityManagerFactory"/
>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"></property>
<property name="database" value="HSQL"></property>
</bean>
</property>
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"></
bean>
</property>
<property name="persistenceUnitName" value="persistenceUnit"></
property>
<!-- property name="persistenceXmlLocation" value="classpath:META-
INF/persistence*.xml"/-->
</bean>
<!-- DI - Injeção de Dependência -->
<bean id="Cliente" class="br.com.frafael.model.Cliente"/>
<bean id="ClienteControler"
class="br.com.frafael.controller.ControllerCliente">
<property name="cli" ref="Cliente"/>
</bean>
<bean id="DAOGenerico"
class="br.com.frafael.impl.GenericDAOImpl" abstract="true">
<property name="entityManagerFactory" ref="entityManagerFactory"></
property>
</bean>
</beans>
continua em outro arquivo que se chama springContextCadastro.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the
DAOs in
- dataAccessContext-local/jta.xml (see web.xml's
"contextConfigLocation").
-->
<beans xmlns="
http://www.springframework.org/schema/beans"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="
http://www.springframework.org/schema/tx"
xmlns:jee="
http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<import resource="springContextDB.xml"/>
<bean id="ClienteDAO" class="br.com.frafael.dao.ClienteDAO"
parent="DAOGenerico"/>
</beans>
============================================================================
Na pasta META-INF tenho meu arquivo persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="persistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</
provider>
<class>br.com.frafael.model.Cliente</class>
</persistence-unit>
</persistence>
========================================================================
Meu DAO Generico que estou fazendo os testes
package br.com.frafael.impl;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import br.com.frafael.interfaces.InterfaceGenericDAO;
public abstract class GenericDAOImp<T> extends JpaDaoSupport
implements InterfaceGenericDAO<T>{
@SuppressWarnings("unchecked")
private Class persistentClass;
@SuppressWarnings("unchecked")
public Class getPersistentClass() {
return persistentClass;
}
@SuppressWarnings("unchecked")
public void setPersistentClass(Class persistentClass) {
this.persistentClass = persistentClass;
}
@SuppressWarnings("unchecked")
public GenericDAOImp(){
this.persistentClass = (Class)((ParameterizedType)getClass
().getGenericSuperclass()).getActualTypeArguments()[0];
}
public void create(T obj){
getJpaTemplate().persist(obj);
}
public void saveorupdate(T obj){
getJpaTemplate().refresh(obj);
}
public void update(T obj){
getJpaTemplate().refresh(obj);
}
public void delete(T obj){
getJpaTemplate().remove(obj);
}
@SuppressWarnings("unchecked")
public List<T> searchList(){
return (List<T>)getJpaTemplate().find("from Cliente order by nome");
}
@SuppressWarnings("unchecked")
public T SearchEntity(int id){
return (T)getJpaTemplate().getReference(this.persistentClass, id);
}
@SuppressWarnings("unchecked")
public List<T> SearchParams(Object[] names,Object[] values,Object[]
ordem){
StringBuffer sql = new StringBuffer("");
sql.append("FROM "+getPersistentClass().getSimpleName()+" ");
//Adicionar filtros
for (int i = 0;i<names.length;i++){
sql.append(names[i]);
}
//Adicionar ordem by
for (int i = 0;i<ordem.length;i++){
sql.append(ordem[i]);
}
return (List<T>)getJpaTemplate().find(sql.toString(),values);
}
}
Mas quando mando executar o projeto surge a seguinte mensagem de erro:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'jpaTemplate' defined in file [C:\java\dsv
\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\pagina
\WEB-INF\classes\springContextDB.xml]: Cannot resolve reference to
bean 'entityManagerFactory' while setting bean property
'entityManagerFactory'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'entityManagerFactory' defined in file [C:\java
\dsv\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
\pagina\WEB-INF\classes\springContextDB.xml]: Invocation of init
method failed; nested exception is java.lang.IllegalArgumentException:
No persistence unit with name 'persistenceUnit' found
Estou vendo no final da mensagem diz:
No persistence unit with name 'persistenceUnit' found
parece que não esta encontrando este arquivo de configuração, tentei
fazer na declaração do meu entityManageFactory o seguinte:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"></property>
<property name="database" value="HSQL"></property>
</bean>
</property>
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"></
bean>
</property>
<property name="persistenceUnitName" value="persistenceUnit"></
property>
<property name="persistenceXmlLocation" value="classpath:META-INF/
persistence*.xml"/>
</bean>
ou seja
<property name="persistenceUnitName" value="persistenceUnit"></
property>
<property name="persistenceXmlLocation" value="classpath:META-INF/
persistence*.xml"/>
mas mesmo assim não encontra.
alguem ja passou por isso e poderia me ajudar?
Grato...