0

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. I thought so long as you provided an ID value, it would update, not make a new one. 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:

@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 ===================================

I'm using MySQL, and the query(s) generated through Hibernate is:

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', ?)
  • I'm suspicious of my extension model. Can you not do that with entities? Article extends Text extends ArtisticExpression extends DatabaseObject – RocketScienceGuy Feb 22 '21 at 07:33
  • What database are you using? Can you also post the SQL that is generated? – Renis1235 Feb 22 '21 at 13:01
  • yea, with "jpa inheritance", you must know (exactly) what you want, or take whatever works :) please see also: https://stackoverflow.com/q/3827494/592355 ..and maybe show one of your extending/concrete classes...and of course: https://en.wikibooks.org/wiki/Java_Persistence/Inheritance – xerx593 Feb 22 '21 at 22:49
  • With MySQL, the @GeneratedValue uses a primary key with 'auto_increment' sequence values. Which means the ID is not inserted, but MySQL generates a unique auto incremented number for you. – blagerweij Feb 22 '21 at 23:55

1 Answers1

0

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?