简体   繁体   English

了解 CrudRepository 保存/更新

[英]Understanding CrudRepository save/update

I've verified that the object I'm trying to update has a specific ID value for the entry I'm trying to replace, and that the object that gets returned is a new entry with a later ID value.我已验证我尝试更新的 object 具有我尝试替换的条目的特定 ID 值,并且返回的 object 是具有更高 ID 值的新条目。 I thought so long as you provided an ID value, it would update, not make a new one.我以为只要您提供一个 ID 值,它就会更新,而不是创建一个新值。 Are there some caveats that I'm missing?是否有一些我遗漏的警告?

    System.out.println("!Parsed article ID = " + article.getID());
    Article returnedArticle = articleRepo.save(article);
    System.out.println("Saved article ID = " + returnedArticle.getID());

outputs:输出:

!Parsed article ID = 1
Saved article ID = 37

Object Def: Object 定义:

@Entity
abstract class DatabaseObject {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

...and other fields in the extending article entity ...和扩展文章实体中的其他字段

=== Update 2/22 17:00EST =================================== === 更新 2/22 17:00EST ====================================

I'm using MySQL, and the query(s) generated through Hibernate is:我正在使用 MySQL,通过 Hibernate 生成的查询是:

Hibernate: 
    select
        article0_.id as id2_0_0_,
        article0_.approved as approved3_0_0_,
        article0_.creators as creators4_0_0_,
        article0_.publish_date as publish_5_0_0_,
        article0_.title as title6_0_0_,
        article0_.text as text7_0_0_ 
    from
        database_object article0_ 
    where
        article0_.id=? 
        and article0_.dtype='Article'
Hibernate: 
    select
        next_val as id_val 
    from
        hibernate_sequence for update
            
Hibernate: 
    update
        hibernate_sequence 
    set
        next_val= ? 
    where
        next_val=?
Hibernate: 
    insert 
    into
        database_object
        (approved, creators, publish_date, title, text, dtype, id) 
    values
        (?, ?, ?, ?, ?, 'Article', ?)

This answer is flawed, or promotes a new question.这个答案有缺陷,或者提出了一个新问题。 Please read it knowing I would still appreciate some help.请阅读它知道我仍然会感谢一些帮助。

After reading much on Transient, Persistent, and Detached states, I found two actionable tasks that improved the situation:在阅读了有关瞬态、持久和分离状态的大量内容后,我发现了两个改善这种情况的可行任务:

Adding a    @Transactional tag to my method, and

Pulling up the old record from the database and modifying it, rather than trying to overwrite what may or may not be present in the database 

    @Transactional
    public ResponseEntity<Article> modifyArticle(@RequestHeader("client-key") UUID clientKey, @RequestBody JsonNode body) { //@RequestBody Article article) {

List<Article> articles = StreamSupport.stream(body.spliterator(), false)
        .map(ticketBody -> parseArticle(ticketBody))
        .collect(Collectors.toList());

        //There should only be one
        Article article = articles.get(0);

    Article returnedArticle;
        Optional<Article> maybeOldArticle = articleRepo.findById(article.getID());
        if (maybeOldArticle.isPresent()) {
            System.out.println("old article is present");
            Article oldArticle = maybeOldArticle.get();
            articleRepo.save(oldArticle);
            article.overwrite(oldArticle);
            returnedArticle = articleRepo.save(oldArticle);
        } else {
            System.out.println("old article is NOT present");
            returnedArticle = articleRepo.save(article);
        }

In the above code, if an entry exists I can overwrite it.在上面的代码中,如果存在条目,我可以覆盖它。 If the entry has been deleted or what-not, it creates a new entry.如果条目已被删除或没有,它会创建一个新条目。 This is not desired.这是不希望的。 How can I use a space for an entry that doesn't exist in the database?如何为数据库中不存在的条目使用空格?

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

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