1
I’m trying to integrate the Spring
with JPA
but so far I have not had success in this configuration. One of the doubts is whether I can configure the bank through the spring-context.xml
. Follow the error I am currently trying to resolve. Javaee Server 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;
Can you tell me if I set up the direct datasource on the Domain server I’m using or can reference it in spring-context?
– Matheus