Spring + Spring JPA Data Configuration

I am currently fooling myself with a Spring installation. My goal is to use JPA to access the Websphere data source using this JNDI name. I use Data JPA to make life easier for me and have worked with some tutorials to get the basic idea.

The bad: none of them talk about Spring configuration for my JPA szenario +. I have never worked with JPA / JDBC before. Therefore, I hope you can help me here. I have two 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 use @Transactual annotion in my code, I use an annotation-driven tag for txManager. I'm just not sure what else I need to configure for txManager and what the sessionFactory tag does. Is there any documentation for all supported XML tags? Am I missing a shortcut for my szenario?

The same goes for eManager - not sure if it is.

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: I don't know what I'm doing. I know that I need a persistence block / provider. I know that many use hibernate for this, but I would like to stay native and use pure JavaEE / Spring, if possible. I just don’t know how to set it up. My project is currently crashing, telling me: "JPA PersistenceProvider returned null"

+3
source share
3

- EntityManagerFactory JNDI Spring JNDI:

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

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

<tx:jta-transactionManager />

, . JpaTransactionManager EntityManagerFactory, JNDI. Spring, <jee:jndi-lookup /> Spring Data JPA. , , , .

+4

Spring, jpa mysql .., . , .

hibernate, , , :)

:

Spring -config.xml:

<context:component-scan base-package="com.MYPACKAGE"/> 
<!-- To find all your controllers -->

<tx:annotation-driven/> 
<!-- This will pickup all your @Transactional annotations-->

<import resource="../database/DataSource.xml"/>
<import resource="../database/Hibernate.xml"/>
<!-- These are my database config files-->

Datasource.xml:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/DATABASENAME"/>
    <property name="username" value="USERNAME"/>
    <property name="password" value="PASSWORD"/>
</bean>

Hibernate.xml:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false"/>
            <property name="generateDdl" value="true"/>
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
        </bean>
    </property>
</bean>

XML-, XML , , ;)

, , ! - , , !

!

+3

, JBoss, jndi persistence.xml :

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

      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/myDS/jta-data-source>

      <class>com.company.model.Document</class>
      <class>com.company.model.DocumentIndividual</class>

      <properties>
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <!-- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> -->
        <property name="javax.persistence.logging.level" value="INFO" />
        <property name="hibernate.show_sql" value="true" />
        <property name="jboss.entity.manager.jndi.name" value="java:/my_em"/>
        <property name="jboss.entity.manager.factory.jndi.name" value="java:/my_emf"/>
    </properties>

</persistence-unit>

, 4.4.2

+1

All Articles