【问题标题】:Spring JPA/Hibernate not updating to DB for change of boolean for @Entity on saveSpring JPA/Hibernate 未更新到 DB 以更改保存时 @Entity 的布尔值
【发布时间】:2016-08-12 17:51:55
【问题描述】:

在某些时候,当我的Repository 保存时,我的应用程序停止为@Entity 保存boolean 值。

调用了REST,在该调用中,我将Device 设置为InUse 为真。

        device.setInUse(inUse);
        Device savedDev = deviceRepository.save(device);

        Logger.debug("Device after save isInUse() " + savedDev.isInUse()); 
        Thread.sleep(5000);

        Device dbDev2 = deviceRepository.findOne(device.getSerial());
        Logger.debug("Device again from db for isInUse() " + dbDev2.isInUse()); 

对于正在保存的设备和发出deviceRepository.findOne(device.getSerial()) 时,此输出为true。问题是当我进入数据库并实际查看它没有更新的记录时。

我打开了休眠调试日志,但我看不到对设备进行的更新。当我保存其他@Entity 时,我可以在日志中看到休眠查询。这让我相信它以某种方式缓存了这个。

在我看到的休眠日志中

  • 包含从休眠中加载的设备列表的父对象
  • (此时应使用 inUse = true 更新设备)
  • 继续从 Hibernate 加载来自父级的 @Entity 列表的其他详细信息,但从不更新设备...

问题

我想知道是什么原因造成的?

休息控制器

@RestController
@Transactional(propagation = Propagation.REQUIRED)
public class HomeController {

    @Autowired
    private DeviceRepository deviceRepository;

    /**
     * Default index
     * 
     * @return
     */
    @RequestMapping(value = "/")
    public String index() {
        return "index";
    }


    @RequestMapping(value = "runTestRun", method = RequestMethod.POST)
    public ResponseEntity<String> runTestRun(@RequestBody String jsonObject) throws Exception {

        .....
            setDevicesInUse(testRun.getDevices(), true);
        .....
    }

private void setDevicesInUse(Set<Device> devices, boolean inUse) throws InterruptedException {
    for (Device device : devices) {
        Logger.debug("Marking Device " + device.getSerial() + " " + device.getReadableName() + " InUse to " + inUse);

        Device dbDev = deviceRepository.findOne(device.getSerial());
        Logger.debug("Device before change checking isInUse() " + dbDev.isInUse()); 
        dbDev.setInUse(inUse);
        Logger.debug("Device after setting isInUse() " + dbDev.isInUse()); 

        device.setInUse(inUse);
        Device savedDev = deviceRepository.save(device);

        Logger.debug("Device after save isInUse() " + savedDev.isInUse()); 
        Thread.sleep(2000);

        Device dbDev2 = deviceRepository.findOne(device.getSerial());
        Logger.debug("Device again from db for isInUse() " + dbDev2.isInUse()); 
        Thread.sleep(2000);
    }   
}

Application.properties

# DataSource settings: set here your own configurations for the database
# connection.
spring.datasource.url: jdbc:mysql://localhost/xxx
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driverClassName=com.mysql.jdbc.Driver

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

# Use hibernates new generator mappings, this is used for the appium port using a TABLE generator
spring.jpa.properties.hibernate.id.new_generator_mappings=true

# Web server
spring.data.rest.base-path=/api
server.context-path=/api
server.port=8082

# TODO FOR DEBUG ONLY
spring.thymeleaf.cache=false
spring.freemarker.cache=false
spring.groovy.template.cache=false
spring.velocity.cache=false
spring.mustache.cache=false
server.session.persistent=true
spring.h2.console.enabled=true
spring.resources.cache-period=0

我尝试添加以下内容,但没有任何区别

spring.jpa.properties.hibernate.cache.use_second_level_cache=false spring.jpa.properties.hibernate.cache.use_query_cache=false

设备

@Data
@Entity
@Table(name = "device")
public class Device {


    private @Id String serial;
    private boolean active;
    private boolean inUse;

    private @Version @JsonIgnore Long version;

    private Device() {
    }

【问题讨论】:

  • 粗略一瞥,闻起来像是交易边界问题。
  • @chrylis 我可以看到父对象从休眠状态加载,然后它应该保存的地方没有任何反应。当我从存储库重新加载设备时,它显示它正在更新,但没有休眠日志显示它实际设置设备 inUse 或实际从数据库中检索保存的设备。

标签: java spring hibernate spring-data-jpa


【解决方案1】:

问题与 Transnational 的设置有关,因为它没有完成交易因此发送到数据库。

@Transactional(propagation = Propagation.REQUIRED)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2018-12-02
    • 2019-09-09
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 2016-06-19
    相关资源
    最近更新 更多