Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring + Spring Data JPA Configuration

currently I'm fooling around with a Spring setup. My goal is to use JPA to get access to a Websphere datasource using it's JNDI name. I'm using Spring Data JPA to make life easier for me and worked through some tutorials to get the basic idea.

Bad thing: none of those is talking about the Spring configuration for my JPA szenario + I never worked with JPA / JDBC before. So I hope you can help me out here. I got 2 configuration files:

applicationContext.xml

<bean id="txManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />

<bean id="eManager" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"></bean>

Since i'm using the @Transactual annotion within my code, i'm using the annotation-driven tag for the txManager. I'm just not really sure what else i should configure for the txManager and what the sessionFactory tag is doing. Is there any documentation for all supported XML tags? Am I missing a importent tag for my szenario?

Same about eManager - not sure if thats right in any way.

persistence.xml

<persistence version="1.0"
    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_1_0.xsd">
    <persistence-unit name="spring-jpa">
        <jta-data-source>jdbc/myJNDI</jta-data-source>
    </persistence-unit>
</persistence>

Same thing here: don't really know what i'm doing. I know i need a persistence unit / provider. I know that many are using hibernate for this, but i would like to stay native and use pure JavaEE / Spring if possible. I'm just not sure how to configure that. Currently my project is crashing, telling me: "JPA PersistenceProvider returned null"

like image 230
omni Avatar asked Apr 22 '26 11:04

omni


1 Answers

The best way is to obtain the EntityManagerFactory from the JNDI via Spring's JNDI support:

<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/myPersistenceUnit" />

<jpa:repositories base-package="com.acme.repositories" />

<tx:jta-transactionManager />

This will cause the transaction manager being used from the application server as well. You can also try to setup a JpaTransactionManager and wire the EntityManagerFactory obtained from JNDI into it. You can pull even more configuration into your Spring config files if you only lookup the datasource through an <jee:jndi-lookup /> namespace element and follow the further configuration instructions in the Spring Data JPA reference documentation. Nevertheless it's usually better to use the container resources you can actually get if you decide to use container resources at all.

like image 156
Oliver Drotbohm Avatar answered Apr 25 '26 03:04

Oliver Drotbohm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!