Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6978165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:43:12+00:00 2026-05-27T17:43:12+00:00

I created relation @ManyToMany with the same entity. Here is my entity: @Entity @Table(name

  • 0

I created relation @ManyToMany with the same entity.

Here is my entity:

@Entity
@Table(name = "user")
public class User extends BaseEntity implements Serializable {

...

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "table_friends", joinColumns =
@JoinColumn(name = "userId"), inverseJoinColumns =
@JoinColumn(name = "friendId"))
private List<User> friends;

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "table_friends", joinColumns =
@JoinColumn(name = "friendId"), inverseJoinColumns =
@JoinColumn(name = "userId"))
private List<User> friendsof;

I created a few “users” and added them to friend list:

User u1 = new User();
User u2 = new User();
User u3 = new User();

u1.getFriends().add(u2);
u1.getFriendsof().add(u2);
u1.getFriends().add(u3);
u1.getFriendsof().add(u3);

When I remove a friend from the list u1.getFriends().remove(u3), everything is ok . But when I want to delete the user from the database u3.remove(), friends of u1 still are the same, although already in the database list of friend is correct.

  1. Can I somehow refresh the list after removing a user?
  2. And if so, how can I know which list of friend I need to refresh?

Edit: Also seems to me that I should remove the (cascade = CascadeType.ALL), because when I remove u1, removes all paired members. Is this the correct behavior?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:43:13+00:00Added an answer on May 27, 2026 at 5:43 pm

    Hmm, why do you need 2 list friends and friendsOf? If you’re my friend, isn’t it obvious that I am your friend?

    Besides, if you only want to end the relationship between 2 User, you should use u1.getFriends().remove(u3).

    Lastly, if you want to remove a user. You should create an EntityManager em and execute the command em.remove(u3). This command will delete u3 as well as the relationship. However, you should be careful with cascade = CascadeType.ALL. This cascadeType means that if you remove u3, u1 will also be removed. I think you should leave it @ManyToMany without cascade.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the BlogPost model : @Entity @Table(name = blog_posts) public class BlogPost extends
I have a class A : @Entity public class A { ... @ManyToMany private
Consider the following two relations: @Entity class Foo { @Id id; @ManyToMany @JoinTable(name =
well it's quiet simple. 2 models with ManyToMany relation: class Artist(models.Model): name = models.CharField(max_length=100,
I have a many to many relation between users and projects: User class: @ManyToMany
I have got a onetomany relation in Hibernate: @Entity public class Project @OneToMany(cascade =
I'm trying to use DQL to create a query between a ManyToMany relation, here
I've created 2 objects: public class Set { public Set() { _sorts = new
I have 2 scaffolds Role(name:string), Permission(name:string) I created a relation between them. So now
I have a many-to-many relation with a linked table. See (simplified) schema below. Created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.