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
JavaBot
JavaBot6mo ago
This post has been reserved for your question.
Hey @Danix! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st6mo ago
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?
Danix
DanixOP6mo ago
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 error
dan1st
dan1st6mo ago
Can you show the stack trace?
Danix
DanixOP6mo ago
@dan1st | Daniel
Danix
DanixOP6mo ago
from this
dan1st
dan1st6mo ago
Is there also an error in the console? Maybe with proper line breaks?
Danix
DanixOP6mo ago
yehh
Danix
DanixOP6mo ago
dan1st
dan1st6mo ago
Can you show RestApplication.getAllBooks? and User?
Danix
DanixOP6mo ago
https://sourceb.in/hmAY9LuT0r here it is
package com.ShelfSpace.ShelfSpace.rest.model;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

@Entity
@Table(name = "User_Info")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long roll_no;

private String name;
private String email;
private String phoneNumber;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonManagedReference
private List<IssuedBooks> issuedBooks;


public User() {}

public User(Long roll_no, String name, String email, String phoneNumber, List<IssuedBooks> issuedBooks) {

this.roll_no = roll_no;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.issuedBooks = issuedBooks;
}
public Long getRoll_no() {
return roll_no;
}
public void setRoll_no(Long roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public List<IssuedBooks> getIssuedBooks() {
return issuedBooks;
}
public void setIssuedBooks(List<IssuedBooks> issuedBooks) {
this.issuedBooks = issuedBooks;
}

}
package com.ShelfSpace.ShelfSpace.rest.model;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

@Entity
@Table(name = "User_Info")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long roll_no;

private String name;
private String email;
private String phoneNumber;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonManagedReference
private List<IssuedBooks> issuedBooks;


public User() {}

public User(Long roll_no, String name, String email, String phoneNumber, List<IssuedBooks> issuedBooks) {

this.roll_no = roll_no;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.issuedBooks = issuedBooks;
}
public Long getRoll_no() {
return roll_no;
}
public void setRoll_no(Long roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public List<IssuedBooks> getIssuedBooks() {
return issuedBooks;
}
public void setIssuedBooks(List<IssuedBooks> issuedBooks) {
this.issuedBooks = issuedBooks;
}

}
here is the user
dan1st
dan1st6mo ago
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?
IssuedBooks user = new IssuedBooks();
Long user2 = user.getUser().getRoll_no();
if (user2==null) {
System.out.println("its null");
}
System.out.println(user.getUser().getRoll_no());
IssuedBooks user = new IssuedBooks();
Long user2 = user.getUser().getRoll_no();
if (user2==null) {
System.out.println("its null");
}
System.out.println(user.getUser().getRoll_no());
Danix
DanixOP6mo ago
the books who gonna issue to the user or student when someone try to add the user details just to debug
dan1st
dan1st6mo ago
well that's the code giving you the error and it doesn't even seem to attempt to do anything useful
Danix
DanixOP6mo ago
yehh but why bcz in the issuedBook table there is roll_no of the user as well
dan1st
dan1st6mo ago
What's IssuedBook?
Danix
DanixOP6mo ago
its the entity
dan1st
dan1st6mo ago
Can you show it?
Danix
DanixOP6mo ago
package com.ShelfSpace.ShelfSpace.rest.model;

import java.time.LocalDateTime;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonBackReference;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;

@Entity
@Table(name = "Issued_Books")
public class IssuedBooks {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long bookId;
private String bookName;
@DateTimeFormat
private LocalDateTime issueDate;
@DateTimeFormat
private LocalDateTime returnDate;

@ManyToOne
@JsonBackReference
@JoinColumn(name = "user_roll_no")
private User user;

public IssuedBooks() {
}

public IssuedBooks( User user, Long bookId , String bookName) {
this.user = user;
this.bookId = bookId;
this.bookName = bookName;
this.issueDate = LocalDateTime.now();
this.returnDate = LocalDateTime.now().plusDays(14);
}


public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public Long getBookId() {
return bookId;
}

public void setBookId(Long bookId) {
this.bookId = bookId;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public LocalDateTime getIssueDate() {
return issueDate;
}

public void setIssueDate(LocalDateTime issueDate) {
this.issueDate = issueDate;
}

public LocalDateTime getReturnDate() {
return returnDate;
}

public void setReturnDate(LocalDateTime returnDate) {
this.returnDate = returnDate;
}



}
package com.ShelfSpace.ShelfSpace.rest.model;

import java.time.LocalDateTime;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonBackReference;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;

@Entity
@Table(name = "Issued_Books")
public class IssuedBooks {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long bookId;
private String bookName;
@DateTimeFormat
private LocalDateTime issueDate;
@DateTimeFormat
private LocalDateTime returnDate;

@ManyToOne
@JsonBackReference
@JoinColumn(name = "user_roll_no")
private User user;

public IssuedBooks() {
}

public IssuedBooks( User user, Long bookId , String bookName) {
this.user = user;
this.bookId = bookId;
this.bookName = bookName;
this.issueDate = LocalDateTime.now();
this.returnDate = LocalDateTime.now().plusDays(14);
}


public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public Long getBookId() {
return bookId;
}

public void setBookId(Long bookId) {
this.bookId = bookId;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public LocalDateTime getIssueDate() {
return issueDate;
}

public void setIssueDate(LocalDateTime issueDate) {
this.issueDate = issueDate;
}

public LocalDateTime getReturnDate() {
return returnDate;
}

public void setReturnDate(LocalDateTime returnDate) {
this.returnDate = returnDate;
}



}
dan1st
dan1st6mo ago
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 it
Danix
DanixOP6mo ago
hmm
dan1st
dan1st6mo ago
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();
Danix
DanixOP6mo ago
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
@PostMapping("/addUser")
public User addUser(@RequestBody User user) {
for(IssuedBooks books : user.getIssuedBooks()) {
books.setIssueDate(LocalDateTime.now());
books.setUser(user);
}
return detailsRepository.save(user);
}

@PostMapping("/addUser")
public User addUser(@RequestBody User user) {
for(IssuedBooks books : user.getIssuedBooks()) {
books.setIssueDate(LocalDateTime.now());
books.setUser(user);
}
return detailsRepository.save(user);
}

can u explain why @dan1st | Daniel
dan1st
dan1st6mo ago
Why are you apssing the user as a @RequestBody? oh nvm it's adding a user the roll number is assigned after saving
Danix
DanixOP6mo ago
Can u elaborate ?
dan1st
dan1st6mo ago
You use @GeneratedValue this means the value is generated when saving it to the DB
JavaBot
JavaBot6mo ago
💤 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.
Want results from more Discord servers?
Add your server