简体   繁体   English

Spring-boot和Hibernate:如何刷新数据?

[英]Spring-boot and Hibernate: How to flush data?

I have a problem with spring-boot + hibernate and my database. 我在使用spring-boot + hibernate和数据库时遇到问题。 When I save an entity with CrudRepository.save(...), sometimes my entity was only saved from JPA, but it's not in the database. 当我使用CrudRepository.save(...)保存实体时,有时我的实体仅从JPA中保存,但不在数据库中。

Thought that the flush mode was not set, i put the following config in the application.yaml but it didn't change anything. 以为没有设置刷新模式,我将以下配置放在application.yaml中,但没有任何改变。 Maybe the mode was not set? 也许没有设置模式?

spring:
   liquibase:
      enabled: true
      change-log: classpath:db/changelog/db.changelog-master.xml
   datasource:
      url: jdbc:postgresql://localhost:26257/mydb?sslmode=disable
      username: myuser
   jpa:
      properties:
         hibernate:
            dialect: org.hibernate.dialect.PostgreSQL94Dialect
            flushMode: ALWAYS
         org:
            hibernate:
              flushMode: ALWAYS
      show-sql: true
      database-platform: org.hibernate.dialect.PostgreSQL94Dialect
      hibernate:
         ddl-auto: create
logging:
   level:
      org.hibernate.SQL: DEBUG
      org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl: ERROR
      org.hibernate.type.descriptor.sql.BasicBinder: TRACE
server:
  port: 8081
@Repository
public interface PermissionRepository extends CrudRepository<Permission, Integer> {

}

Any idea? 任何想法? Thanks :) 谢谢 :)

First of all, I see in application.yml that you use liquibase that leds me to think you use it to manage your db. 首先,我在application.yml看到您使用liquibase,这使我认为您使用它来管理数据库。 Soo, firstly you shouldn't set spring.jpa.hibernate.ddl-auto to create, but validate , I think. 如此,首先,您不应该将spring.jpa.hibernate.ddl-auto设置为创建,而是要validate :) :)

Then it would be great if you show us the entity/entities and the method where you use the crudrepository. 那么,如果您向我们展示使用crudrepository的实体/实体和方法,那就太好了。

So, it's up to your entity ie is there any @OneToMany or @ManyToOne relation? 因此,这取决于您的实体,即是否存在@OneToMany或@ManyToOne关系? Do you use transactionManager(s)? 您是否使用transactionManager?

Regarding jpa hibernate flushMode settings : It all depends on how you bootstrap Hibernate. 关于jpa hibernate flushMode settings :这完全取决于您如何引导Hibernate。 Entity queries should trigger a flush on both legacy and JPA modes with the default flushMode (AUTO). 实体查询应使用默认的flushMode(AUTO)在旧模式和JPA模式上触发刷新。

In addition, maybe you should read this ticket as well with the answer of Vlad Mihalcea. 此外,也许您也应该阅读弗拉德·米哈尔西娅(Vlad Mihalcea)的答案的这张票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM