Hibernate doesn't save a list of authorities of a user to table as separate rows. It saves only one.
I need help to save several roles of a user to database. Using Spring Boot JPA. User can have many roles. A role belongs to one user. I tried to create three users - an admin, a user and a manager. Admin has admin role, user has user role. Manager has both - admin and user roles.
I have Authority class that has a role. The problem is that for the manager hibernate creates only last role - USER. And I expected that it will have both roles. In database I see that manager has only 1 row. And it should have 2 rows. What am I doing wrong?
User:
Authority:
21 Replies
⌛
This post has been reserved for your question.
Hey @Tomasm21! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
Creating users:
Database:
Creating Users service:
QUESTION: Why the manager in the database has only 1 USER authority instead of two? I added 2 roles for him - admin and user. But in the DB it has only user. What I was doing wrong?
@Tomasm21 What is your database?
MariaDB
HeidiSql is its Windows client
Are you working from a fresh database everytime? Like why not just seed the database with your users and such
I think the database is fresh. It runs in the memory since the boot up.
I don't seed database manually
I need that Spring would add users the way I defined
because React.js will send data from website and spring will have to work like a charm
Maybe the way I add role is not right?
Maybe this operation is immutable?
single
repo save
can I run it in loop
You can run it in a loop
Try that?
Wait I'm dumb, auth is being reused
edit
You need to put the
new Authority()
into the for loop, otherwise you're using the same objectmm.. ok lets see
I thought we can edit the same object and just to change its property
No, you want new objects
Each object represents a row in the database, so when you mutated the same object, you were changing what was going to be saved.
@Crain With your function code hibernate does not save Authorities. Only Users. But I understood idea that I need to create new Authority objects in order to add new row to the table. The service code to create new user is this:
First you have to save new user and get its object from database. It will have user id. then to use returned object to create his authorities and add to auth Repository.
result:
Manager id 25.
I'm surprised my code didn't work, but I'm glad the change fixed it
Authority table now has 2 rows with constraint column values 25
Thank you.
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Post Closed
This post has been closed by <@312509109863710732>.