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;
}
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.
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.
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.
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.
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”.
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.
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.
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.
I finally found the solution... the entity I was trying to update had the @Immutable annotation on it...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With