Java Spring引导jpa数据使用错误的数据库
我有一个SpringBoot应用程序,它拉入一个jar,它有两个数据库配置,都是mysql数据库。这两个数据库在日志中看起来都已正确启动,但没有唯一标识。看起来默认值已注册,然后又重新注册,因此它似乎要覆盖Java Spring引导jpa数据使用错误的数据库,java,spring,spring-data,spring-data-jpa,Java,Spring,Spring Data,Spring Data Jpa,我有一个SpringBoot应用程序,它拉入一个jar,它有两个数据库配置,都是mysql数据库。这两个数据库在日志中看起来都已正确启动,但没有唯一标识。看起来默认值已注册,然后又重新注册,因此它似乎要覆盖 2017-07-19 10:24:16,817 INFO DriverManagerDataSource - Loaded JDBC driver: com.mysql.jdbc.Driver [tx-id=] 2017-07-19 10:24:16,937 INFO LocalCon
2017-07-19 10:24:16,817 INFO DriverManagerDataSource - Loaded JDBC driver: com.mysql.jdbc.Driver [tx-id=]
2017-07-19 10:24:16,937 INFO LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'default' [tx-id=]
2017-07-19 10:24:16,947 INFO LogHelper - HHH000204: Processing PersistenceUnitInfo [
name: default
...] [tx-id=]
2017-07-19 10:24:16,991 INFO Version - HHH000412: Hibernate Core {5.0.12.Final} [tx-id=]
2017-07-19 10:24:16,992 INFO Environment - HHH000206: hibernate.properties not found [tx-id=]
2017-07-19 10:24:16,993 INFO Environment - HHH000021: Bytecode provider name : javassist [tx-id=]
2017-07-19 10:24:17,021 INFO Version - HCANN000001: Hibernate Commons Annotations {5.0.1.Final} [tx-id=]
2017-07-19 10:24:17,379 INFO Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect [tx-id=]
2017-07-19 10:24:17,593 WARN RootClass - HHH000038: Composite-id class does not override equals(): com.example.EmployeeGroupEntity$EmployeeGroupId [tx-id=]
2017-07-19 10:24:17,593 WARN RootClass - HHH000039: Composite-id class does not override hashCode(): com.example.EmployeeGroupEntity$EmployeeGroupId [tx-id=]
2017-07-19 10:24:17,943 INFO LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'default' [tx-id=]
2017-07-19 10:24:17,970 INFO DriverManagerDataSource - Loaded JDBC driver: net.sourceforge.jtds.jdbc.Driver [tx-id=]
2017-07-19 10:24:17,972 INFO LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'default' [tx-id=]
2017-07-19 10:24:17,972 INFO LogHelper - HHH000204: Processing PersistenceUnitInfo [
name: default
...] [tx-id=]
2017-07-19 10:24:18,370 INFO Dialect - HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect [tx-id=]
2017-07-19 10:24:18,389 INFO LobCreatorBuilderImpl - HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 [tx-id=]
2017-07-19 10:24:18,394 WARN RootClass - HHH000038: Composite-id class does not override equals(): com.example.EmployeeGroupEntity$EmployeeGroupId [tx-id=]
2017-07-19 10:24:18,394 WARN RootClass - HHH000039: Composite-id class does not override hashCode(): com.example.entity.EmployeeGroupEntity$EmployeeGroupId [tx-id=]
2017-07-19 10:24:18,405 WARN EntityManagerFactoryRegistry - HHH000436: Entity manager factory name (default) is already registered. If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name' [tx-id=]
我有两个用于配置数据库连接的类,它们都位于导入的jar中,基本上是一个公共库
@Configuration
@EnableTransactionManagement
@PropertySource(value = { "classpath:/${app.execution.environment}/application.properties" })
@EnableJpaRepositories(basePackages = "com.example", entityManagerFactoryRef = "mysqlEntityManager", transactionManagerRef = "mysqlTransactionManager")
@EntityScan(basePackages = { "com.example" })
public class MysqlHibernateConfig {
// beans configured here marked with @Primary
...
}
及
我尝试通过以下类从每个数据库获取数据:
@Transactional("mysqlTransactionManager") // workspace complains about not being able to find this bean
@Service
public class PriceService {
}
@Transactional("mysqlTransactionManager2") // workspace complains about not being able to find this bean
@Service
public class PriceService2 {
}
PriceService可以很好地获取数据,但PriceService2尝试从mysqlTransactionManager而不是从mysqlTransactionManager获取数据
我还有以下类来运行我的启动应用程序:
@Configuration
@ComponentScan(basePackages = "com.example")
public class WebMvcConfig extends WebMvcConfigurerAdapter {
//config stuff
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
如何强制一个类使用特定的事务管理器/数据源?我一直在尝试各种配置,据我所知,这是正确设置的。如果我把这两个数据源配置类都放进我的spring boot应用程序中,我就可以连接到正确的数据源,没有问题。但当我把它们搬到外面的图书馆时,它就不起作用了。由于我的项目的结构,我必须将它们保存在一个公共库中,两个单独的应用程序使用此数据库配置
如果必要的话,我可以自由地将非主db移动到我的spring boot应用程序中,但这也不起作用。结果也是一样。在这里,您似乎在jpa存储库级别遇到了冲突。相同的存储库包由不同的
EntityManager
|TransactionManager
覆盖,因此您应该将存储库放在不同的包中,并在不交叉的情况下重新配置存储库。此外,我还会检查EntityManager
|TransactionManager
的bean声明。我在那里放了一些示例,但我的数据源配置类中basePackages的实际结构更像这个com.example和com.example.com.u包,它们是分开的,但一个可能重叠,第一个更通用。编辑将更接近我真正想要的have@Sergi如果我尝试将实体移动到另一个包中,我会在所有已移动的实体上获得“非托管类型”。尝试将@EntityScan添加到指向新包的我的配置中,但仍然无法让它拾取新实体。
@Configuration
@ComponentScan(basePackages = "com.example")
public class WebMvcConfig extends WebMvcConfigurerAdapter {
//config stuff
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}