Spring boot Spring boot JPA多数据源未更新或创建表
我面临一个JPA问题,spring boot具有Spring boot Spring boot JPA多数据源未更新或创建表,spring-boot,jpa,groovy,Spring Boot,Jpa,Groovy,我面临一个JPA问题,spring boot具有多个数据源。这是我一直努力做到的事情。但这一次我不明白为什么不工作 在gradle构建或引导运行之后,不会创建或更新任何表。启动时没有编译或运行时错误。我疯了 你可以在附件中找到我的代码 P2BDatabaseConfig.groovy @Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef = "p2bE
多个数据源
。这是我一直努力做到的事情。但这一次我不明白为什么不工作
在gradle构建或引导运行之后,不会创建或更新任何表。启动时没有编译或运行时错误。我疯了
你可以在附件中找到我的代码
P2BDatabaseConfig.groovy
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "p2bEntityManagerFactory",
transactionManagerRef = "p2bTransactionManager",
basePackages = {"it.project.sol.sharpapi.repo.p2b"}
)
public class P2BDatabaseConfig {
@Bean(name = "p2bDataSource")
@ConfigurationProperties(prefix = "spring.p2b")
@Primary
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@PersistenceContext(unitName = "p2bPU")
@Bean(name = "p2bEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean p2bEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("p2bDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("it.project.sol.sharpapi.entity.p2b").build();
}
@Bean(name = "p2bTransactionManager")
@Primary
public PlatformTransactionManager p2bTransactionManager(
@Qualifier("p2bEntityManagerFactory") EntityManagerFactory p2bEntityManagerFactory) {
return new JpaTransactionManager(p2bEntityManagerFactory);
}
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "sharpEntityManagerFactory",
transactionManagerRef = "sharpTransactionManager",
basePackages = {"it.project.sol.sharpapi.repo.sharp"}
)
public class SharpDatabaseConfig {
@Bean(name = "sharpDataSource")
@ConfigurationProperties(prefix = "spring.sharp")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@PersistenceContext(unitName = "sharpPU")
@Bean(name = "sharpEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean sharpEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("sharpDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("it.project.sol.sharpapi.entity.sharp").build();
}
@Bean(name = "sharpTransactionManager")
public PlatformTransactionManager sharpTransactionManager(
@Qualifier("sharpEntityManagerFactory") EntityManagerFactory sharpEntityManagerFactory) {
return new JpaTransactionManager(sharpEntityManagerFactory);
}
}
SharpDatabaseConfig.groovy
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "p2bEntityManagerFactory",
transactionManagerRef = "p2bTransactionManager",
basePackages = {"it.project.sol.sharpapi.repo.p2b"}
)
public class P2BDatabaseConfig {
@Bean(name = "p2bDataSource")
@ConfigurationProperties(prefix = "spring.p2b")
@Primary
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@PersistenceContext(unitName = "p2bPU")
@Bean(name = "p2bEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean p2bEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("p2bDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("it.project.sol.sharpapi.entity.p2b").build();
}
@Bean(name = "p2bTransactionManager")
@Primary
public PlatformTransactionManager p2bTransactionManager(
@Qualifier("p2bEntityManagerFactory") EntityManagerFactory p2bEntityManagerFactory) {
return new JpaTransactionManager(p2bEntityManagerFactory);
}
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "sharpEntityManagerFactory",
transactionManagerRef = "sharpTransactionManager",
basePackages = {"it.project.sol.sharpapi.repo.sharp"}
)
public class SharpDatabaseConfig {
@Bean(name = "sharpDataSource")
@ConfigurationProperties(prefix = "spring.sharp")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@PersistenceContext(unitName = "sharpPU")
@Bean(name = "sharpEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean sharpEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("sharpDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("it.project.sol.sharpapi.entity.sharp").build();
}
@Bean(name = "sharpTransactionManager")
public PlatformTransactionManager sharpTransactionManager(
@Qualifier("sharpEntityManagerFactory") EntityManagerFactory sharpEntityManagerFactory) {
return new JpaTransactionManager(sharpEntityManagerFactory);
}
}
application.yml
spring:
profiles:
active: Developement
jpa:
show-sql: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: update
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
dialect: org.hibernate.dialect.MySQL5Dialect
p2b:
url: jdbc:mysql://localhost:3306/p2bv2?autoReconnect=true
username: xxxx
password: xxxx!
testWhileIdle: true
maxActive: 5
validationQuery: SELECT 1
driver-class-name: com.mysql.jdbc.Driver
sharp:
url: jdbc:mysql://localhost:3306/sharp?autoReconnect=true
username: xxxx
password: xxxx!
testWhileIdle: true
maxActive: 5
validationQuery: SELECT 1
driver-class-name: com.mysql.jdbc.Driver
P2BDevice.groovy
@Entity(name = "P2BDevice")
@Table(name = "device")
class P2BDevice implements Serializable{
@Id
@GeneratedValue
Long id
@Column(name = "version")
Long version
@Column(name = "date_created")
Date dateCreated
@Column(name = "deleted")
int deleted
@Column(name = "description")
String description
...
}
User.groovy
@Entity(name = "User")
@Table(name = "caccapupu")
class User implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id
@Column(name = "version")
Long version
@Column(name = "username")
String username
@Column(name = "password")
Long password
@Column(name = "date_created")
Date dateCreated
@Column(name = "status")
int status
...
}
我可以向您保证,存储库是正确的,甚至我的类的包位置也是正确的。尝试显式设置JPA属性
LocalContainerEntityManagerFactoryBean em =
builder.dataSource(dataSource).packages("it.project.sol.sharpapi.entity.sharp").build();
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
em.setJpaPropertyMap(properties);
LocalContainerEntityManagerFactoryBean em=
builder.dataSource(dataSource.packages(“it.project.sol.sharpapi.entity.sharp”).build();
HashMap属性=新建HashMap();
properties.put(“hibernate.hbm2ddl.auto”、“update”);
properties.put(“hibernate.dialogue”、“org.hibernate.dialogue.mysql5dialogue”);
em.setJpaPropertyMap(属性);
这很有效,谢谢!但我不明白为什么它不能从我的应用程序中自动选取这些属性。ymlI不知道spring boot如何在内部引导JPA,也许它不能将java配置与yml/prop混合使用,或者它只适用于默认的JPA bean名称,即“entityManagerFactory”