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

喜欢而已 提交于 2020-05-14 15:57:47

问题


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;
}

回答1:


In my (limited) experience these types of problems are a tad more difficult to debug because of the magic happening behind the scenes with Spring, but I'll try to give some advice that has helped me troubleshoot similar problems - hopefully this gives you the nudge you need.

The @Transactional notation establishes a transactional scope that dictates when a transaction starts and ends, also called its boundary. If you operate outside of this boundary you'll either receive errors or things just won't work as expected.

First off, I found this bit of documentation the most helpful on Spring transactions: http://docs.spring.io/spring-framework/docs/4.2.x/spring-framework-reference/html/transaction.html specifically this section

Secondly, you may wish to enable trace level logs and potentially the SQL statements to help debug this. In order to do so I added the following to my application.properties - You'll be able to add the same into your application.yml with some minor adjustments

spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.type=trace
spring.jpa.show-sql=true
logging.level.org.hibernate=TRACE

There will be ALOT of output here, but you'll get a good idea of whats happening behind the scenes.

Third, and the most important part for me in learning how to use @Transactional is that every call to the DAO creates a new session -or- reuses the existing session if within the same transactional scope. Refer to the documentation above for examples of this.

Now, I suspect that your transaction is still considered open when you're reading back your updated object. Try doing a flush in your save method and see if that helps persist your changes back to the database.

Hopefully you'll be able to find the issue without too much hunting using the resources above, if not you should at the very least have some more complete information surrounding why the persistence isn't persisting.




回答2:


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




回答3:


I think your problem is in the interception that you do with the annotation @UserCanCud.

I just made a project with the same settings that you propose and it works correctly. Take a look at:

https://github.com/nitzap/spring-data-mysql

If I'm missing something, just let me know.




回答4:


To save your object you need to setup proper jpa configurations and the repository after that it will be save.

for example we have user entity and userRepository as below

User Enity Class

package com.sanjeev.domain;

import javax.persistence.*;

@Entity(name = "user")
@Table(name = "user")
public class User {

    @Id
    @Column(name = "id")
    private Long id;
    @Column(nullable = false)
    private String username;
    @Column(nullable = false)
    private String email;
    @Column(nullable = false)
    private String password;

 //getters and setters   
}

User Repository class

package com.sanjeev.repository;    
    import com.sanjeev.domain.User;
    import org.springframework.data.jpa.repository.JpaRepository;

    public interface UserRepository extends JpaRepository<User, Long> {

    }

User Service class

package com.sanjeev.service;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.sanjeev.repository.UserRepository;
import com.sanjeev.domain.User;

@Service
public class UserService{

  @Autowired
  private UserRepository userRepository;

  public void saveUser(User user){
    userRepository.save(user);
  }


}

If you will follow above steps then your object must me saved

for more click here



来源:https://stackoverflow.com/questions/43966901/spring-boot-jpa-hibernate-does-not-commit-when-repository-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!