Using Spring as a JPA Container

Posted by sdoca on Stack Overflow See other posts from Stack Overflow or by sdoca
Published on 2010-06-09T20:47:35Z Indexed on 2010/06/09 20:52 UTC
Read the original article Hit count: 295

Filed under:
|
|

Hi,

I found this article which talks about using Spring as a JPA container:

http://java.sys-con.com/node/366275

I have never used Spring before this and am trying to make this work and hope someone can help me.

In the article it states that you need to annotate a Spring bean with @Transactional and methods/fields with @PersistenceContext in order to provide transaction support and to inject an entity manager.

Is there something the defines a bean as a "Spring Bean"? I have a bean class which implements CRUD operations on entities using generics:

@Transactional
public class GenericCrudServiceBean implements GenericCrudService
{
    @PersistenceContext(unitName="MyData")
    private EntityManager em;

    @Override
    @PersistenceContext
    public <T> T create(T t)
    {
        em.persist(t);
        return t;
    }

    @Override
    @PersistenceContext
    public <T> void delete(T t)
    {
        t = em.merge(t);
        em.remove(t);
    }
...
...
...
    @Override
    @PersistenceContext
    public List<?> findWithNamedQuery(String queryName)
    {
        return em.createNamedQuery(queryName).getResultList();
    }
}

Originally I only had this peristence context annotation:

@PersistenceContext(unitName="MyData")
private EntityManager em;

but had a null em when findWithNamedQuery was invoked. Then I annotated the methods as well, but em is still null (no injection?).

I was wondering if this had something to do with my bean not being recognized as "Spring".

I have done configuration as best I could following the directions in the article including setting the following in my context.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:tx="http://www.springframework.org/schema/tx"
    tx:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="MyData" />
        <property name="dataSource" ref="dataSource" />
        <property name="loadTimeWeaver"
            class="org.springframework.classloading.ReflectiveLoadTimeWeaver" />
        <property name="jpaVendorAdapter" ref="jpaAdapter" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:MySID" />
        <property name="username" value="user" />
        <property name="password" value="password" />
        <property name="initialSize" value="3" />
        <property name="maxActive" value="10" />
    </bean>

    <bean id="jpaAdapter"
        class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
        <property name="databasePlatform"
            value="org.eclipse.persistence.platform.database.OraclePlatform" />
        <property name="showSql" value="true" />
    </bean>

    <bean
        class="org.springframework.ormmjpa.support.PersistenceAnnotationBeanPostProcessor" />
    <tx:annotation-driven />
</beans>

I guessed that these belonged in the context.xml file because the article never specifically said which file is the "application context" file. If this is wrong, please let me know.

© Stack Overflow or respective owner

Related posts about spring

Related posts about jpa