简体   繁体   English

保存多个实体时,Spring JPA插入错误的列

[英]Spring JPA inserting to wrong column when saving multiple entities

I have encountered an issue where I save a list of entities all at once, sometimes some rows have values being written to wrong columns. 我遇到了一个问题,我一次保存所有实体列表,有时某些行的值写入了错误的列。

Basically, I have a Movie entity, which extends Show (annotated with @MappedSuperclass ), which extends TraceableEntity that is also annotated with @MappedSuperclass as shown below: 基本上,我有一个Movie实体,它扩展了Show (用@MappedSuperclass注释),并扩展了TraceableEntity ,它也用@MappedSuperclass注释,如下所示:

@MappedSuperclass
@EntityListeners(TraceableEntity.TraceableEntityListener.class)
public abstract class TraceableEntity {
    @Column
    private Date createdOn;

    @Column
    private Date dateUpdated;

    public Date getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    public Date getDateUpdated() {
        return dateUpdated;
    }

    public void setDateUpdated(Date dateUpdated) {
        this.dateUpdated = dateUpdated;
    }

    public static class TraceableEntityListener {
        @PrePersist
        public void beforeInsert(TraceableEntity entity) {
            if (entity.getCreatedOn() == null) {
                entity.setCreatedOn(new Date());
            }
        }

        @PreUpdate
        public void beforeUpdate(TraceableEntity entity) {
            entity.setDateUpdated(new Date());
        }
    }
}

Now, on some occasions, the value of createdOn ends up in dateUpdated , as shown in this screenshot . 现在,在某些情况下,createdOn的值dateUpdated结束,如本屏幕截图

In a nutshell, my application is a scraper that retrieves data from an API. 简而言之,我的应用程序是一个从API检索数据的抓取工具。 I'm using RestTemplate in CompletableFuture to download data concurrently, and then save everything in one go. 我在CompletableFuture使用RestTemplate同时下载数据,然后一次性保存所有内容。 The method in which .save(...) is invoked is annotated with @Transactional . 调用.save(...)的方法带有@Transactional注释。 When the size of the list is under approximately 1500 the saving is fine, it seems that things go wrong when the size exceeds 1500 for some reason. 当列表的大小小于1500时,保存就可以了,当大小超过1500时,出于某种原因,似乎出错了。 I'd really appreciate your time and help in this matter! 非常感谢您的宝贵时间,并在此方面为您提供帮助!

Does it always happen at the same place? 是否总是在同一地方发生? Are you missing some rows for example? 例如,您是否缺少某些行? perhaps the text of some of the stuff you're scraping has special characters that were not escaped properly? 也许您要抓取的某些内容的文本中包含无法正确转义的特殊字符? You may want to turn on your logging to see exactly what is being sent to the server. 您可能需要打开日志记录,以查看发送到服务器的确切信息。

The reason that if you scrape just the "problematic" movies and it turns out fine is probably because the actual problem occurred before the movies in question. 如果仅刮擦“有问题的”电影而证明正常,则可能是因为实际问题发生在所讨论的电影之前。

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

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