Integration between Spring MVC and JPA

1

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;  
    
asked by anonymous 27.10.2016 / 19:22

2 answers

1

I was able to solve my problem by creating a dataSorce in the domain of my Server (GlassFish) and referencing in persistence.xml

It looks like this:

spring-context.xml

<!--  ***** JPA CONFIGURATION  ***** -->

<!-- LocalEnityManagerFactoryBean para criar o EntityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <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>

<!-- 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  ***** -->

persistence.xml

 <persistence-unit name="centralPU" transaction-type="JTA">

   <!-- provedor/implementacao do JPA -->
   <provider>org.hibernate.ejb.HibernatePersistence</provider>

   <jta-data-source>jdbc/CentralDS</jta-data-source>

Here is the tutorial used to create A MYSQL DATASOURCE IN GLASSFISH

  

Obs : If the GlassFish server is used, add the mysql Lib   in this folder glassfish\domains\seu_domain\lib\ext

    
31.10.2016 / 20:15
2

This error is happening because in your persistence.xml file, you entered the transaction-type attribute as "RESOURCE_LOCAL", which must be "JTA". Only by changing this configuration you can inject the EntityManager through annotation @PersistenceContext, which is how the JPA specification says. As for the rest of the setup I can not tell if it's correct, because I use spring-boot and this is more abstract.

    
28.10.2016 / 17:09