I'm trying to integrate Spring
with JPA
but so far I have not had success in this configuration. One of the doubts is if I can configure the bank through spring-context.xml
. Here is the error that I am trying to solve at the moment. Server JavaEE glassfish3
.
Error
java.lang.RuntimeException: The persistence-context-ref-name [br.com.central.dao.DefaultDaoImpl/entityManager] in module [central] resolves to a persistence unit called [centralPU] which is of type RESOURCE_LOCAL. Only persistence units with transaction type JTA can be used as a container managed entity manager. Please verify your application.
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="centralPU" transaction-type="RESOURCE_LOCAL">
<!-- provedor/implementacao do JPA -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- entidade mapeada -->
<class>br.com.central.bean.Cidade</class>
<properties>
<!-- propriedades do hibernate -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<!-- atualiza o banco, gera as tabelas se for preciso -->
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
spring-context.xml
<!-- ***** JPA CONFIGURATION ***** -->
<!-- LocalEnityManagerFactoryBean para criar o EntityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="centralPU" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>
<!-- JpaTransactionManager to manager JPA transactions -->
<bean class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/central" />
<property name="username" value="root" />
<property name="password" value="connect123" />
</bean>
<!-- Instrui o Spring a realizar gerenciamento @Transactional automático nas classes anotadas -->
<tx:annotation-driven/>
<!-- Realizar injeção de recursos de acordo com a especificação JPA (@PersistenceContext, @PersistenceUnit). -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<!-- Realizar a conversão de exceções nas classes @Repository (das exceções nativas como JPA PersistenceExceptions to Spring's DataAccessException). -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!-- ***** JPA CONFIGURATION ***** -->
Dao
@Repository
@Transactional
public abstract class DefaultDaoImpl<T> implements DefaultDao<T>{
@PersistenceContext
protected EntityManager entityManager;