Hibernate Mapping
I want to map two entities with one to many relationships and want to add the data together. Then how to do it ? Can anyone suggest !
27 Replies
⌛
This post has been reserved for your question.
Hey @Danix! 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 your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
In what ways do you want to add the data together?
What's the input? What's the expected output? What did you try and how does it not work?
Ok basically I tried to make 2 entities 1st is students and 2nd is issued_books and the student entity is connected to issued books through one to many mapping and issued book is connected with student entity with many to one mapping
. And after that I make a user repository from jpa and make a controller to add the student who are issuing books and save the student like this
userrepo.save(user)
but the data doesn't added when I tried to add the Json from postman to the db and give 500 errorCan you show the stack trace?
@dan1st | Daniel
from this
Is there also an error in the console?
Maybe with proper line breaks?
yehh
Can you show
RestApplication.getAllBooks
?
and User
?If you create a new
IssuedBooks
using the no-args constructor, there is no user with a roll number
What's IssuedBooks
?
and what's that supposed to do?
the books who gonna issue to the user or student when someone try to add the user details
just to debug
well that's the code giving you the error
and it doesn't even seem to attempt to do anything useful
yehh but why bcz in the issuedBook table there is roll_no of the user as well
What's
IssuedBook
?its the entity
Can you show it?
ok
So in the controller, you call
IssuedBooks user = new IssuedBooks();
this creates an IssuedBooks
with no data meaning there isn't even a user in ithmm
then you call
getUser()
in it but it doesn't have a user associated with it
and then you call getRoll_no()
on the nonexisting user which doesn't work
Your issue is the IssuedBooks user = new IssuedBooks();
hmm i got it now but i have a little confusion about this one bcz i when i tried to save the user without setting up the
@JsonManagedReference and @JsonBackReference
to the entities the in the issue Book table dont get the user roll no but when i use it then i dont need to set the user to the issued Book table like this can u explain why
@dan1st | DanielWhy are you apssing the user as a
@RequestBody
?
oh nvm it's adding a user
the roll number is assigned after savingCan u elaborate ?
You use
@GeneratedValue
this means the value is generated when saving it to the DB💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.