Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

JPA configuration - picks up wrong datasource

$
0
0
I have application A that calls library B.

Each has its own database, datasource, jpa configuration, persistence unit and dao. All the names are unique to A.

What happens:
- A creates its tables in B's database!
- A appears to use the correct persistence unit (since it creates the right tables).
- A is using B's datasource, and *might* be using B's EntityManagerFactory and TransactionManager.

What did I miss in A's configuration to ensure that it uses its own configuration files?

Context:
Code:

<beans
  xmlns=              "http://www.springframework.org/schema/beans"
  xmlns:xsi=          "http://www.w3.org/2001/XMLSchema-instance"
  xmlns:ctx=          "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>

  <ctx:annotation-config />
  <ctx:component-scan base-package="mydomain.a.db" />

  <bean id="aDatasource" ...> (points to A's database)
  ...

  <bean id="aDao"                      class="mydomain.a.db.ADaoImpl" />

  <bean id="aEMF"                      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation"  value="classpath*:aPersistence.xml"/>
    <property name="dataSource"              ref="aDataSource" />
    <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="databasePlatform"    value="org.hibernate.dialect.SQLServerDialect"/>
      </bean>
    </property>
  </bean>

  <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
  <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

  <tx:annotation-driven transaction-manager="aTM" proxy-target-class="false" />
  <bean id="aTM"                      class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory"    ref="aEMF" />
  </bean>
</beans>

aPersistence.xml:
Code:

<?xml version="1.0" encoding="UTF-8"?>
<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="aUnit" transaction-type="RESOURCE_LOCAL">
    <class>mydomain.a.db.mapping.AMappingClass</class>
  </persistence-unit>
</persistence>

ADaoImpl:
Code:

@Repository
public class ADaoImpl implements ADao {

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

  @Override
  @Transactional(value="aTM")
  public void persist(AMappingClass aMappingObj) {
    ...
  }
}


Viewing all articles
Browse latest Browse all 297

Trending Articles