2

I know we can use spring's PropertyPlaceholderConfigurer bean in spring xml file which reads specified properties file and use values in xml file. Like wise is there a way where we can use this mechanism in my persistence.xml file.

Can i use org.eclipse.persistence.jpa.PersistenceProvider in datasource bean like this in spring xml file?

    <bean id="dataSource"
    class="org.eclipse.persistence.jpa.PersistenceProvider">
    <property name="javax.persistence.jdbc.driver" value="${datasource.driverClassName}" />
    <property name="javax.persistence.jdbc.url" value="${datasource.url}" />
    <property name="javax.persistence.jdbc.user" value="${datasource.username}" />
    <property name="javax.persistence.jdbc.password" value="${datasource.password}" />
</bean>

<bean id="entityManager"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:./META-INF/persistence.xml"/>
    <property name="persistenceUnitName" value="JPAService"/>
    <property name="dataSource" ref="dataSource"/>

</bean>

Thanks in Advance.

Prathap
  • 1,023
  • 7
  • 19
  • 33

2 Answers2

0

Rather than using your build to create a prod or dev version of your persistence.xml, just move all property settings to your spring content.

read the original post by emeraldjava loading .properties in spring-context.xml and persistence.xml

Community
  • 1
  • 1
Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
  • refer sprng doc http://static.springsource.org/spring/docs/2.0.x/reference/orm.html#orm-jpa-multiple-pu – Hemant Metalia Jan 13 '12 at 07:27
  • @Hemanth : No use iam getting below exception : `Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager] for property 'persistenceUnitManager': no matching editors or conversion strategy found` – Prathap Jan 13 '12 at 07:42
0

Like I said in my comment, the first part is not possible, check this SO question

Concerning the second part: yes, that'll work. We use a separate datasource.xml file though and import it into the application context for better modularity.

spring-context.xml:

<import resource="classpath:datasouce.xml" />

datasource.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
                        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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd  
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
    default-autowire="byName">

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

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

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />

</beans>
Community
  • 1
  • 1
Pete
  • 10,720
  • 25
  • 94
  • 139
  • By using the second way in my code, I am getting below exception `Caused by: java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified` . My xml file declared as ` ` – Prathap Jan 13 '12 at 08:31
  • Try to add this: ` ` – Pete Jan 13 '12 at 08:44
  • I did that as well,now i get below : `Caused by: java.lang.IllegalStateException: Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation.` – Prathap Jan 13 '12 at 08:45
  • Hi Pete, By adding below class it works `class="org.springframework.instrument.classloading.SimpleLoadTimeWeave` But what exactly is **loadTimeWeaver** – Prathap Jan 13 '12 at 08:49
  • Hm... Can't say much about that, you'll have to google it ;) Good to know it works. For reference I edited my post and included our complete `datasource.xml` which we import into our `spring-context.xml` and that's it. – Pete Jan 13 '12 at 08:52