Reputation: 8922
I m trying to use multiple datasources using Springboot and JPA, but I m having errors when trying to start my server.
The problem only occurs when I try to use my second data source. I m having the following error when trying to start my application :
Not an managed type: class com.company.app.backoffice.modelDocument.Category
All is working great for the first data source. But it seems that my second entity manager doesn't track the good package. For example, I need my first datasource to manage my model package, and my second to manage modelDocument package :
<!-- Configure the data source bean -->
<!-- Website datasource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Doc base datasource -->
<bean id="docDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.doc.url}"/>
<property name="username" value="${jdbc.doc.username}"/>
<property name="password" value="${jdbc.doc.password}"/>
</bean>
<!-- Configure the entity manager factory bean -->
<!-- Website Entity manager -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.company.app.backoffice.model"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Doc base Entity manager -->
<bean id="docEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="docDataSource"/>
<property name="packagesToScan" value="com.company.app.backoffice.modelDocument"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Configure the transaction manager bean -->
<!-- Website transation manager -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- Doc base transaction manager -->
<bean id="docTransactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="docEntityManagerFactory"/>
</bean>
Here's the class I need to manage in the second data source, which is in the modelDocument package :
@Entity
@Table(name = "category")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String name;
public Long getId() {
return id;
}
public String getName() {
return name;
}
}
Does anybody knows what's going wrong with this ?
EDIT : Implicit repository to manage entity persistence
package com.company.app.backoffice.repository;
import com.company.app.backoffice.modelDocument.Category;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CategoryRepository extends JpaRepository<Category, Long> {
}
EDIT 2 : The controller where the repository is injected :
@Controller
public class CategoryController {
@Autowired
private CategoryRepository categoryRepository;
@RequestMapping(value = "/categories", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public List<Category> categories() {
return categoryRepository.findAll();
}
}
Upvotes: 0
Views: 99
Reputation: 27048
When you have multiple datasources, you will have to declare separate transaction manager (PlatformTransactionManager
) and entity manager (LocalContainerEntityManagerFactoryBean
). So Springboot gets confused. To resolve this use @Primary
annotation. Put this annotation on any one of the transaction manager and on any one of the entity manager
Cheers
Upvotes: 0
Reputation: 3089
The main problem is that you have two different datasources and entity managers to work with JPARepository. I think Spring get lost when you work this way. Is is working only with one datasource/em. I have looked for a solution to set in Spring the em/datasource to one specific JPARepository, and I havent found. Despite that, you could follow this tutorial to create your own JPARepository implementation and define two classes. Each one to work with different datasource and EntityManager. Doing this, you should achieve what you want.
Upvotes: 0
Reputation: 11017
add package scan containing the entities
<jpa:repositories base-package="your.package.enties" />
Upvotes: 2