Integration between Spring MVC and JPA

Asked

Viewed 380 times

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;  

2 answers

2

This error is happening because in your persistence.xml file, you entered the attribute transaction-type as "RESOURCE_LOCAL", which should be "JTA". Only by changing this setting you can inject Entitymanager through the Annotation @Persistencecontext, which is how the JPA specification says. As for the rest of the configuration I do not know if it is correct, because I use spring-boot and this is more abstract.

  • 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?

1


I managed to solve my problem by creating a dataSorce on my server (Glassfish) and referencing on persistence.xml

Stayed 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 the tutorial used of how to create A MYSQL DATASOURCE IN GLASSFISH

Obs: If the Glassfish server is already used, add the mysql Lib in that folder glassfish\domains\seu_domain\lib\ext

Browser other questions tagged

You are not signed in. Login or sign up in order to post.