JPARepository findAll() loop data - spring

I have 3 entities:
SLUSER_INFO(SLUSER_INFO_ID, FULLNAME, FIRSTNAME, LASTNAME, ADDRESS, DOB)
SLUSER(SLUSER_ID, PASSWORD, USERNAME, SLUSER_INFO_ID)
SLROLE(SLROLE_ID, ROLENAME, SLUSER_ID)
In User entity:
#OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
#JsonView(SlUserView.Full.class)
private List<SlRole> roles;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "SLUSER_INFO_ID", referencedColumnName = "SLUSER_INFO_ID")
#JsonView(SlUserView.Full.class)
private SlUserInfo userInfo;
In Role entity:
#ManyToOne
#JoinColumn(name = "SLUSER_ID", referencedColumnName = "SLUSER_ID")
private SlUser user;
and User_info entity
#OneToOne(mappedBy = "userInfo")
private SlUser user;
I create the rest api to get all user by using JPARepository findAll
#Repository
public interface SlUserRepository extends JpaRepository<SlUser, Long> {
}
Controller
public ResponseEntity<Page<SlUser>> findAll(Pageable pageable) {
Page<SlUser> pageResult = usersRepository.findAll(pageable);
return new ResponseEntity<Page<SlUser>>(pageResult, HttpStatus.OK);
}
try to run the rest api, the results are duplicated data.
{"content":[{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":{"id":1,"username":"v7001","password":"admin","roles":[{"id":1,"roleName":"ADMIN"}],"userInfo":{"id":1,"fullName":"","firstName":"Jayce","lastName":"Sonar","address":"ad1","dob":null,"user":
Please give me advise

The error is because of the bi-directional relationship between User and Roles.
Use #JsonIgnore on roles in User entity.

Related

Confused why getting a User from Repository fixed "failed to lazily initialize a collection of role" compared to using SecurityContextHolder

My goal was to pass a List of Businesses to the model from the controller to display it in a view and I have succeeded, but have a bit of confusion.
When I initially tried using:
public User getCurrentAuthenticatedUser() {
UserDetailsImpl user = (UserDetailsImpl) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return user.getUser();
}
#GetMapping("")
public String list(Model model) {
model.addAttribute("businesses", userService.getCurrentAuthenticatedUser().getBusinesses());
return "business/list";
}
I got this error: "failed to lazily initialize a collection of role: com.xyz.User.businesses could not initialize proxy - no Session"
Then I tried:
#GetMapping("")
public String list(Model model) {
int userId = userService.getCurrentAuthenticatedUser().getId();
User user = userService.getById(userId); // gets User using Spring Data JPA UserRepository
List<Business> businesses = user.getBusinesses();
model.addAttribute("businesses", businesses);
return "business/list";
}
And this worked perfectly fine.
What was the issue using the first method. It seemed more simple rather than calling a User from the UserRepository. I've seen some posts that say you should use EAGER fetching, but that's just seems like a bandaid solution.
From the beginner's understanding: Since fetch type is LAZY the businesses don't exist yet in the User but are fetched on demand later on so there shouldn't be an issue.
Edit: After more thought I remembered that with basic Hibernate you would have to create Transactions and commit transactions. I'm assuming that User is not within a Transaction that's why I can't get businesses using the 1st method.
What would be a better solution to fetch the current Authenticated user? And that user's attributes such as a list of businesses.
Model Classes:
Business:
#Entity
#Table(name = "businesses")
public class Business {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
private LocalDate date;
#ManyToOne(cascade={CascadeType.MERGE})
#JoinColumn(name="user_id")
private User user;
public Business() {
}
public Business(String name, String description, LocalDate date, User user) {
...
}
public Business(Long id, String name, String description, LocalDate date, User user) {
...
}
... getters/setters
}
USER:
#Entity
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
private String password;
private boolean enabled;
#ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
#JoinTable( name = "users_roles",
joinColumns = #JoinColumn(name = "user_id"),
inverseJoinColumns = #JoinColumn(name = "role_id"))
private Set<Role> roles = new HashSet<>();
#OneToMany(fetch = FetchType.LAZY, mappedBy="user", cascade={CascadeType.MERGE})
private List<Business> businesses;
... getters/setters
}

JPA OneToOne and shared primary key need manual assignment

I'm using Springboot and JPA to create two tables sharing the same primary key.
For the first table I write:
public class UserAccount implements Serializable
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#OneToOne(mappedBy ="user", cascade = {CascadeType.REMOVE, CascadeType.MERGE,
CascadeType.REFRESH}, fetch=FetchType.LAZY)
#PrimaryKeyJoinColumn
private UserLogin login;
}
For the second table I write:
public class UserLogin implements Serializable
{
#Id
private Long user_id;
#OneToOne(cascade = {CascadeType.MERGE, CascadeType.REFRESH},
fetch=FetchType.LAZY)
#MapsId("user_id")
#JoinColumn(name = "user_id", referencedColumnName = "id")
#Setter(AccessLevel.NONE)
private UserAccount user;
public void setUser(UserAccount user)
{
this.user = user;
this.user_id = user.getId();
}
}
Other stuff are omitted for conciseness. The code works because I manually set the id of UserLogin by writing the statement
this.user_id = user.getId();
otherwise I get the error:
Hibernate error: ids for this class must be manually assigned before calling save():
I guess that the ids can be manually managed but I cannot get the right configuration.
UPDATE:
I found the solution thanks (see the accepted answer). Now I just would get rid of the findById() when setting the user login.
//these methods are defined within a dedicated #Service
#Transactional
public void createLoginInfo(UserAccount user)
{
UserLogin userlogin=new UserLogin();
this.addLoginToUser(userlogin,user);
loginService.save(userlogin);
}
#Transactional
public void addLoginToUser(UserLogin login, UserAccount account)
{
//whit this commented line works
//UserAccount acc= this.findById(account.getId());
login.setUser(account);
account.setLogin(login);
}
//In a transactional test method I first create the user then I call
userService.save(theuser);
userService.createLoginInfo(theuser);
You have a bidirectional relationship, but have mapped it with a few competing options that don't work well together. First, in UserAccount, it isn't clear why you have an ID that is generated, yet try to also map it with the relationship (specifically using a PrimaryKeyJoinColumn). If you want it generated, it can't also be a foreign key value in a reference - and you've already got this relationship setup as the 'other' side via the 'mappedBy' setting. It should just be:
public class UserAccount implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#OneToOne(mappedBy ="user", cascade = {CascadeType.REMOVE, CascadeType.MERGE,
CascadeType.REFRESH}, fetch=FetchType.LAZY)
private UserLogin login;
}
User login then should just be:
public class UserLogin implements Serializable {
#Id
private Long user_id;
#OneToOne(cascade = {CascadeType.MERGE, CascadeType.REFRESH},
fetch=FetchType.LAZY)
#MapsId("user_id")
#JoinColumn(name = "user_id", referencedColumnName = "id")
#Setter(AccessLevel.NONE)
private UserAccount user;
public void setUser(UserAccount user) {
this.user = user;
}
}
Note because you have the mapsId annotation on the user relationship, JPA will set the user_id property for you once the ID is assigned - there is no need to manually set it yourself. You can, but if you do you must insure it was assigned previously - which requires a save/flush on the UserAccount. If you don't actually use the Long user_id property, you don't really even need to map it; you can just mark the user property as the ID:
public class UserLogin implements Serializable {
#Id
#OneToOne(cascade = {CascadeType.MERGE, CascadeType.REFRESH},
fetch=FetchType.LAZY)
#JoinColumn(name = "user_id", referencedColumnName = "id")
#Setter(AccessLevel.NONE)
private UserAccount user;
public void setUser(UserAccount user) {
this.user = user;
}
}
The Long ID from UserAccount then can be used to lookup UesrAccounts and UserLogin instances.
Try this :
public class UserLogin implements Serializable
{
#Id
private Long user_id;
#OneToOne(fetch=FetchType.LAZY)
#MapsId
#JoinColumn(name = "user_id")
private UserAccount user;
public UserAccount getUser() {
return user;
}
public void setUser(UserAccount user) {
this.user = user;
}
}
public class UserAccount implements Serializable
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
To persist UserLogin :
EntityManager em;
UserAccount user = em.find(UserAccount.class, 1L)
UserLogin login = new UserLogin();
login.setUser(user);
em.persist(login);

How can i get all my saved posts in spring boot?

I have to entities like User and Food. User can save food posts. I am trying to getting all saved posts of user but how can i do this? I am writing hibernate query in food repository but i can't access saved food posts.
Here is my code:
#Data
#Entity
public class User extends BaseEntity {
#Column(unique = true, nullable = false)
private String username;
#JsonIgnore
#OneToMany
private List<Food> savedRecipes;
}
Food class:
#Data
#Entity
#Where(clause = "deleted = false")
public class Food extends BaseEntity {
private String foodName;
private String recipe;
#OneToMany
private List<Category> categoryList;
#ManyToOne(fetch = FetchType.EAGER)
#JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
#JoinColumn(name = "user_id", referencedColumnName = "id")
private User user;
}
Repository Code:
#Repository
public interface FoodRepository extends JpaRepository<Food,Long> {
List<Food> findAllByFoodNameContaining(String searchedValue);
List<Food> findAllByCategoryListInAndDeletedFalse(List<Category> categoryList);
List<Food> findAllByUserId(Long id);
List<Food> findAllByUserSavedRecipes(Long id);
}
Try this way.
List <Food> findAllByUser (User user);
You can easily get All User's saved food by call below methods
1st: List<Food> result = User.getSavedRecipes()
2nd: List<Food> result = FoodRepository.findAllByUserId(Long id)

Pageable not giving expected results with #ManyToMany relationship

We are dealing with #ManyToMany relation with Users and Roles and want to have pagination to get all the Users with associated Roles by using Pageable interface. It is only considering the records count for pagination on the User Table and Roles table record is not considered. But ideally in RDBMS the actual record count would be after flattening the result of join between Users and Roles table.
When working with Pageable in findAll method and passing the page configuration as below :
pageno: 0 and pageSize:1
Pageable paging = PageRequest.of(0, 1);
userRepository.findAll(paging);
It is giving the result as below 
Technically there are 3 records when we flatten the result but pageable is considering this as 1 record which is not correct. Is this intended behavior?
Is there a way where we can get the pagination after flattening the result set of query?
Yes. This is intended. Data is mapped to Java objects as nested objects. Hence, pageable of 5 user records will return 5 users irrespective of number of roles each user has.
To restrict pagination based on record count by combination of user and role, you have to add join between user and role to the query in repository method and fetch columns from both user and role (like we do in SQL).
Below code works for me
User entity
public class User
{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long userId;
#NonNull
#Column(unique = true, name= "user_name")
private String userName;
#NonNull
private String password;
#NonNull
private boolean status;
#NonNull
private boolean passwordExpired;
#ManyToMany(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
#JoinTable(name = "user_role", joinColumns = {
#JoinColumn(name = "userId", referencedColumnName = "userId") }, inverseJoinColumns = {
#JoinColumn(name = "role_name", referencedColumnName = "name") })
#BatchSize(size = 20)
private Set<Role> roles = new HashSet<>();
//Get and set
}
Role Entity
public class Role {
private static final long serialVersionUID = 1L;
#NotNull
#Size(max = 50)
#Id
#Column(length = 50,unique=true)
private String name;
//get and set
}
Repository
#Repository
public interface UserRepo extends JpaRepository<User, Long>
{
#Query(value="SELECT u.userName,r.name FROM User u left join u.roles r")
public ArrayList<User> findByrole(Pageable paging);
}
Service method
public ArrayList<User> findByrole()
{
// TODO Auto-generated method stub
Pageable paging = PageRequest.of(0, 4);
return uRepo.findByrole(paging);
}

how to insert data in one to many relationship in spring jpa

I am new in Spring. I want to insert the data into the AUDIT table. There is a one-two-many relationship between USER and AUDIT tables.
For every login and logout data should be inserted into the AUDIT table. How can I do that?
The user entity:
#Table(name="USER")
public class User {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="USERID")
private Long id;
private String username;
private String email;
private Long phone;
private String password;
private int OTP;
private boolean activation_flag;
#OneToMany(cascade=CascadeType.ALL)
#JoinColumn(name="USERID")
private Set<Audit> audit;
#ManyToOne
#JoinColumn(name="ADDRESSID")
private Address address;
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(
name = "USER_AUTHORITY",
joinColumns = {#JoinColumn(name = "USER_ID", referencedColumnName = "USERID")},
inverseJoinColumns = {#JoinColumn(name = "AUTHORITY_ID", referencedColumnName = "ID")})
private List<Authority> authorities;
The audit entity:
#Entity
#Table(name="AUDIT")
public class Audit {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="AUDITID")
private long auditid;
#Column(name="GEOLOCATION")
private String geolocation;
#Column(name="ACTION")
private String action;
#Column(name="DATETIME")
private Date datetime;
#Column(name="DEVICEID")
private long deviceid;
#Column(name="ACTIONSUCCESS_FAIL")
private boolean actionsuccess_fail;
#Column(name="JWT_TOKEN")
#Type(type="text")
private String JWT_token;
#ManyToOne
private User user;
The audit repository:
public interface AuditRepository extends JpaRepository<Audit, Long> {
}
The user repository:
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
User findByEmail(String email);
User findByPhone(Long phone);
User findById(Long id);
}
Add the audit to the user you want the audit associated with, then save.

Resources