lundi 2 mars 2015

Spring + JPA + multiple persistence units: Injecting EntityManager

I need to use one database for queries (non-modifying) and one for commands (modifying). I am using Spring Data JPA, so I have two configuration classes:



@Configuration
@EnableJpaRepositories(value = "com.company.read",
entityManagerFactoryRef = "readingEntityManagerFactory",
transactionManagerRef = "readingTransactionManager")
@EnableTransactionManagement
public class SpringDataJpaReadingConfiguration {

@Bean(name = "readingEntityManagerFactory")
public EntityManagerFactory readingEntityManagerFactory() {
return Persistence.createEntityManagerFactory("persistence.reading");
}

@Bean(name = "readingExceptionTranslator")
public HibernateExceptionTranslator readingHibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}

@Bean(name = "readingTransactionManager")
public JpaTransactionManager readingTransactionManager() {
return new JpaTransactionManager();
}

}

@Configuration
@EnableJpaRepositories(value = "com.company.write",
entityManagerFactoryRef = "writingEntityManagerFactory",
transactionManagerRef = "writingTransactionManager")
@EnableTransactionManagement
public class SpringDataJpaWritingConfiguration {

@Bean(name = "writingEntityManagerFactory")
public EntityManagerFactory writingEntityManagerFactory() {
return Persistence.createEntityManagerFactory("persistence.writing");
}

@Bean(name = "writingExceptionTranslator")
public HibernateExceptionTranslator writingHibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}

@Bean(name = "writingTransactionManager")
public JpaTransactionManager writingTransactionManager() {
return new JpaTransactionManager();
}

}


In my repository I sometimes need to decide with EntityManager to use like so:



@Repository
public class UserReadingRepository {

@PersistenceContext(unitName = "persistence.reading")
private EntityManager em;

// some useful queries here
}


I am using persistence unit's name as defined in my persistence.xml:



<persistence xmlns="http://ift.tt/UICAJV"
xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/UICAJV http://ift.tt/O9YdEP"
version="2.0">

<persistence-unit name="persistence.reading" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>ReadingDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>

<persistence-unit name="persistence.writing" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>WritingDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>

</persistence>


Spring throws org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'persistence.reading' is defined. Oddly, it looks like Spring tries to instantiate a bean with persistence unit name? Did I misconfigure something?


Aucun commentaire:

Enregistrer un commentaire