Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot + JPA + Hibernate does not commit when repository.save

I have a Spring Boot Application + JPA with MySQL. In my controller, when I post an entity I am able to call to repository.save, and I return the "created/updated" object.

But, when I look at the database, I see that the object is not updated.

Here is my application.yml:

spring:
  jpa:
    show-sql: true
    generate-ddl: false
    hibernate:
      ddl-auto: none
    properties:
      hibernate.dialect: org.hibernate.dialect.MySQLDialect
      org.hibernate.envers.store_data_at_delete: true
      org.hibernate.envers.global_with_modified_flag: true
      org.hibernate.envers.track_entities_changed_in_revision: true
  datasource:
    initialize: false
    url: jdbc:mysql://${DB_HOST:localhost}:${DB_PORT:3306}/${DB_NAME:databaseName}?createDatabaseIfNotExist=true
    username: ${DB_USERNAME:root}
    password: ${DB_PASSWORD:root}
    driver-class-name: com.mysql.jdbc.Driver
    hikari:
      minimumIdle: 20
      maximumPoolSize: 30
      idleTimeout: 5000
      data-source-properties:
        cachePrepStmts: true
        prepStmtCacheSize: 250
        prepStmtCacheSqlLimit: 2048

Do you know what else do I have to do?

This is my controller:

@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public MyEntity saveMyEntity(@Valid @RequestBody final MyEntity myEntity) {
    Assert.notNull(myEntity, "The entry cannot be null");
    return myEntityService.save(myEntity);
}

And MyEntityService:

@Override
@UserCanCud
public Entity save(final Entity entity) {
    Assert.notNull(entity);
    final Entity savedEntity = repository.save(entity);
    return savedEntity;
}
like image 707
Manuelarte Avatar asked May 14 '17 17:05

Manuelarte


People also ask

How do you check if JPA save was successful?

I want to know how to check if save was success or not? You can check it by using if (person != null) and return a response. The documentation says entity will never be null.

How do I commit to a JPA Repository?

Commit a transaction by calling the commit() method on the Connection interface. This tells your database to perform all required consistency checks and persist the changes permanently. Rollback all operations performed during the transaction by calling the rollback() method on the Connection interface.

What is difference between save and saveAndFlush in JPA?

The saveAndFlush() Method Unlike save(), the saveAndFlush() method flushes the data immediately during the execution. This method belongs to the JpaRepository interface of Spring Data JPA.

Does JPA flush commit?

JPA also defines a COMMIT flush mode, which is described as follows: If FlushModeType. COMMIT is set, the effect of updates made to entities in the persistence context upon queries is unspecified. When executing a JPQL query, the persistence context is only flushed when the current running transaction is committed.

What is JPA save method in hibernate?

Save The save method is an “original” Hibernate method that does not conform to the JPA specification. Its purpose is basically the same as persist, but it has different implementation details. The documentation for this method strictly states that it persists the instance, “first assigning a generated identifier”.

What is jparepository () method in spring data JPA?

This method belongs to the JpaRepository interface of Spring Data JPA. Here's how we use it: Normally, we use this method when our business logic needs to read the saved changes at a later point during the same transaction but before the commit.

What is the difference between save () and saveorupdate () methods in hibernate?

Summary 1 Save () method stores an object into the database. ... 2 SaveOrUpdate () calls either save () or update () on the basis of identifier exists or not. ... 3 Probably you will get very few chances to actually call save () or saveOrUpdate () methods, as hibernate manages all changes done in persistent objects.

What is the use of save () method in Spring Boot?

3. The save () Method As the name depicts, the save () method allows us to save an entity to the DB. It belongs to the CrudRepository interface defined by Spring Data. Let's see how we can use it: Normally, Hibernate holds the persistable state in memory. The process of synchronizing this state to the underlying DB is called flushing.


1 Answers

I finally found the solution... the entity I was trying to update had the @Immutable annotation on it...

like image 134
Manuelarte Avatar answered Oct 26 '22 20:10

Manuelarte