• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Why does not it work SequenceGenerator in Spring Boot?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I make a blog on Spring Boot + Spring Date
There is a post, user, comment, and entities that contain links between them.
For each of these 6 entities, I added the annotation


Also created in the Database additionally hibernate_sequencе

However, the following problems arise.
1) When I add a post (with id = 1) and delete it, and then create a new post, it is already with id 2, not id 1
2) When I try to add a comment to it,then throws an error that usually occurs if there is no SequenceGenerator.
ERROR: insert or update on table "posts_comments" violates foreign key constraint "posts_comments_post_id_fkey"
DETAIL: Key (post_id) = (5) is not present in table

Why?

add comment in CommentService

 

Comment

 


Post


   


User


   

 
Sheriff
Posts: 22118
114
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Степан Адамов wrote:1) When I add a post (with id = 1) and delete it, and then create a new post, it is already with id 2, not id 1


That's how sequences work. It doesn't give you the highest available value + 1. It just gives you the sequence's own next value. It (usually) starts with 1, and every time you ask the sequence for a new value (id), it increments its internal value. So no matter what you do, you will always get 1, 2, 3, etc. If you decide to discard some of these values, the sequence doesn't know, and will just give you the next value based on what it has already returned.

2) When I try to add a comment to it,then throws an error that usually occurs if there is no SequenceGenerator.
ERROR: insert or update on table "posts_comments" violates foreign key constraint "posts_comments_post_id_fkey"
DETAIL: Key (post_id) = (5) is not present in table

Why?


You'll need to provide some more information here. How do you create the post and add it to the comment? Which entities do you save? Because I didn't see any persist cascades on the relation in the Comments class. That means that if you don't save the Post instance separately, it will not be persisted
 
Stepan Adamov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I add 1 post, then the comment is quietly added.
And if the post has already been added twice, then an error pops up
 
Saloon Keeper
Posts: 23411
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Stepan!

Would you please transliterate your display name (Stepan Adamov)? While we have a very international community here, to avoid confusion, we prefer that people standardise on the English language and the Latin alphabet. We have all the fun we can stand just trying to decipher stack traces in Portuguese! Doesn't matter if you prefer "Stepan", "Stephan" or something different, we'd just like to have your name readable by those who don't know the Cyrillic alphatbet.

Rob should be able to easily help you with your problem as long as you can supply him with the things he needs to see.
 
Stepan Adamov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Welcome to the Ranch, Stepan!

Would you please transliterate your display name (Stepan Adamov)? While we have a very international community here, to avoid confusion, we prefer that people standardise on the English language and the Latin alphabet. We have all the fun we can stand just trying to decipher stack traces in Portuguese! Doesn't matter if you prefer "Stepan", "Stephan" or something different, we'd just like to have your name readable by those who don't know the Cyrillic alphatbet.

Rob should be able to easily help you with your problem as long as you can supply him with the things he needs to see.



Ok, i changed
 
Tim Holloway
Saloon Keeper
Posts: 23411
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Спасибо!
 
Rob Spoor
Sheriff
Posts: 22118
114
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stepan Adamov wrote:If I add 1 post, then the comment is quietly added.


That's probably because of your cascades. Post.listComments cascades all operations, including persist.

And if the post has already been added twice, then an error pops up


You'll have to show the error so I can be certain, but I'm guessing that it's a unique constraint or primary key violation, because the code attempts to insert the same post twice.
 
Beware the other head of science - it bites! Nibble on this message:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic