[英]Hibernate sequence does not exist error after adding second Database source
在我的項目中添加第二個數據源后,我遇到以下錯誤:
Table 'portal-titan.hibernate_sequence' doesn't exist; error performing isolated work; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: error performing isolated work
當我嘗試插入帶有類型的 object 時出現,包括GenerationType.AUTO 。 我有點困惑,因為這個話題有很多問題和討論,我嘗試了很多,但我沒有得到想要的結果。 當我將 GenerationType 更改為 IDENTITY 時,它開始工作,但我讀到這會導致性能問題,這不是預期的結果。 更重要的是,我在 yml 文件中的 hibernate 屬性中有use-new-id-generator-mappings: false ,但這也無助於解決問題。
這是我的 yml 文件:
management:
security:
roles: ADMIN
context-path: /management
spring:
messages:
basename: i18n/messages
mvc:
favicon:
enabled: false
thymeleaf:
mode: XHTML
jpa:
hibernate:
ddl-auto: validate
use-new-id-generator-mappings: false
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
format-sql: true
physical_naming_strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
implicit_naming_strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
caching:
specs:
products:
timeout: 60
orders:
timeout: 60
max: 500
deliveries:
timeout: 120
tracking:
timeout: 1
admins:
timeout: 120
balance:
timeout: 120
application:
async:
core-pool-size: 2
max-pool-size: 50
queue-capacity: 1000
jwt:
token-secret: secret-key
token-validity: PT6H
token-remember-me-validity: P7D
default-language-tag: bg
upload:
allowed-content-types:
- image/jpg
- image/jpeg
- image/png
static-resource:
path: /static/
jobs:
batch-size: 20
activity:
purge:
ttl-value: 90
ttl-unit: days
job-run-interval-value: 1
job-run-interval-unit: days
這是現在想要插入的實體的外觀:
@Getter
@Setter
@Entity
@Table(name = "comments")
public class Comment implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(nullable = false, unique = true)
private String uuid;
@Column(nullable = false)
private String content;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "delivery_id")
private Delivery delivery;
@CreatedDate
@Column(name = "created_at", nullable = false)
private Instant createdAt = Instant.now();
@LastModifiedDate
@Column(name = "updated_at", nullable = false)
private Instant updatedAt = Instant.now();
}
這是插入 controller 部分的方法:
@PostMapping("/{deliveryUuid}")
@ApiOperation(value = "Submit a comment")
@ApiResponses(
value = {
@ApiResponse(code = 201, message = "Comment created"),
@ApiResponse(code = 400, message = "Validation failed")
})
@PreAuthorize("hasRole('ROLE_CUSTOMER')")
@ResponseStatus(value = HttpStatus.CREATED)
public void submitComment(
@PathVariable("deliveryUuid") String deliveryUuid,
@Valid @RequestBody CommentDto commentDto,
@CurrentUser AuthUser principal) {
commentService.submitComment(commentDto, deliveryUuid, principal);
}
因為在我配置第二個數據庫后出現錯誤啟動器,所以我也在添加他們的代碼。 評論實體在主數據庫中。
初級:
@Configuration
@EnableTransactionManagement
@EnableJpaAuditing
@EntityScan(basePackageClasses = {TitanClientApp.class})
@EnableJpaRepositories(
entityManagerFactoryRef = "clientEntityManagerFactory",
transactionManagerRef = "clientTransactionManager",
basePackages = { "titan.client" }
)
public class DbConfiguration {
@Primary
@Bean(name="clientDataSource")
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource clientDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "clientEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean clientEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("clientDataSource") DataSource clientDataSource) {
return builder
.dataSource(clientDataSource)
.packages("titan.client")
.build();
}
@Primary
@Bean(name = "clientTransactionManager")
public PlatformTransactionManager clientTransactionManager(
@Qualifier("clientEntityManagerFactory") EntityManagerFactory clientEntityManagerFactory) {
return new JpaTransactionManager(clientEntityManagerFactory);
}
}
次要:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "gpsEntityManagerFactory",
transactionManagerRef = "gpsTransactionManager",
basePackages = {"titan.gps"}
)
public class SecondaryDbConfiguration {
@Bean(name = "gpsDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource gpsDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "gpsEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean gpsEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("gpsDataSource") DataSource gpsDataSource) {
return builder
.dataSource(gpsDataSource)
.packages("titan.gps")
.build();
}
@Bean(name = "gpsTransactionManager")
public PlatformTransactionManager gpsTransactionManager(
@Qualifier("gpsEntityManagerFactory") EntityManagerFactory gpsEntityManagerFactory) {
return new JpaTransactionManager(gpsEntityManagerFactory);
}
}
您的第二個數據庫只是缺少 Hibernate 需要正常工作的表。 如果要使用基於表的序列,則必須創建該表,這是默認設置。
只要您不每秒插入數千條記錄,使用IDENTITY
就完全可以。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.