0

I'm working on a spring and JPA project. I had configured my JPA Persistence Unit in the Persistence.xml and here's my spring configuration file.

My application works fine, but I didn't understand how does spring framework detects the Persistence Unit defined in my Persistence.xml file and injects it without being defined in my spring bean configuration file .

Can anybody answer me please ?

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="ma.professionalpartners.fireAppBusiness.dao"/> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="fireApp-Domain" />
    </bean>

    <bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"  />
    </bean>

    <tx:annotation-driven transaction-manager="jpaTransactionManager" />

</beans>

2 Answers2

0

You have provided the name for the persistence unit, when configuring the entityManagerFactory bean:

<property name="persistenceUnitName" value="fireApp-Domain" />
dunni
  • 43,386
  • 10
  • 104
  • 99
  • Thank's for responding me dunni, yes but I didn't defined a bean with id="fireApp-Domain" in my spring configuration file, It's just the name of the PU in the persistence.xml. Is there any relation between the Persistence.xml and the spring configuration file ? – Abdessamad Boutgayout Jan 20 '15 at 10:13
  • You don't need to define a bean. Spring looks for the persistence.xml in the default locations (META-INF/persistence.xml). If it finds the persistence unit with the name you gave, it will create it. – dunni Jan 20 '15 at 13:10
0

The persistence.xml file MUST be on certain paths, so that Spring simply searched in those locations. After finding the file, it parses the XML content, and if there is a single PersistenceUnit, that is made the default one. Of course, if you specify a name (as you did), then it looks exactly for that PersistenceUnit.

V G
  • 18,822
  • 6
  • 51
  • 89