How to update a List of Users in the User class Spring Boot

miko34565

I have a Java User class, a user can have friends (List<User>). By default, Hibernate create two tables : USER and USER_FRIENDS(USER_ID,FRIENDS_ID)

The problem is when I change friends in my code and that I save(user), spring add the new friends but don't remove in the database the friends removed from the array list.

@Entitypublic class User implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String pseudo;
private String password;
private String email;
private Gender gender;
@Lob
private byte[] avatar;
private String description;
private Date birthdate;

@ManyToMany(cascade = CascadeType.ALL)
private List<Game> favoriteGames = new ArrayList<>();

@OneToMany( cascade = CascadeType.ALL)
private List<User> friends = new ArrayList<>();

I tried @ManyToMany, @OneToMany, cascade = CascadeType.ALL

David Mališ

Basically, first I would advise that you take special care with your equals and hashCode implementation in your entities. You did not show us that, but it should be something like this in your User.java:

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (!(o instanceof User)) {
        return false;
    }
    User other = (User) o;
    return id != null && id.equals(other.getId());
}

@Override
public int hashCode() {
    return getClass().hashCode();
}

Those are very important, especially when working with entities in collections.

Secondly, a connection between a User and his Friends (other Users) should be modeled as Many-to-Many, because:

  • every user can be a friend to MANY of other users
  • every user can have any number of friends, in other words MANY friends

And I would model this connection like this:

@ManyToMany
@JoinTable(name = "user_friends", joinColumns = @JoinColumn(name = "user_id"),
        inverseJoinColumns = @JoinColumn(name = "friend_user_id"))
private Set<User> friends = new HashSet<>();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to update user with PUT spring boot and JPA

How to update details of farmer in spring boot through the UserServiceImpl class?

Vaadin4Spring's ManagedSecurity: How to update user list?

How to map getBody array list response of RestTemplate into class in Spring boot?

How to see specify class list in spring-boot?

How to update database with spring boot

Spring Boot: How to Update Beans?

Unable to read list of user defined class from application.yml file in a Java Spring Boot project

How to query users by user id in spring security?

How to configure Spring Boot Security so that a user is only allowed to update their own profile

Spring Boot 2.2.6 - Security | How to update Principal after updating user's information

Get list of users from another realm in Keycloak Spring boot

How to get google user email to whitelist users when authenticating using Spring Boot OAuth2 against Google

Spring Security/Spring Boot - How to set ROLES for users

Spring Boot - How do I invalidate a successful authentication in AuthenticationFilter based on list of returned user authorities

How to apply server side validation on List of Object in spring boot? spring not validating List<Address> which is present in Employee Class

Spring Boot : how to update object efficently?

How to update all libraries in Spring Boot project?

How to update data on Principal object on Spring Boot

How to enable or disable user in Spring Boot Security

How to populate dropdown list from one entity class to another in spring-boot?

How to find a particular class object from a List of Object in Spring boot Dependency Injection

spring boot - how to avoid "Failed to instantiate [java.util.List]: Specified class is an interface" in HTTP controller handler?

Spring Boot - how make different resources for different users

How to handle spring boot stomp websocket subscribe endpoint to authorize users

Spring boot API - How to scale an App as users grow

how avoid to access to other data users. Spring boot + MongoDB

How to track login of users in a Java Spring Boot application

How can i get the liste of all users with roles in spring boot?