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 8999077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:07:53+00:00 2026-06-16T00:07:53+00:00

I am trying to persist a a relationship between users and friends with a

  • 0

I am trying to persist a a relationship between users and friends with a many to many self join. I am using the following method, but no relation is ever created in the database. The user passed a method parameter is created from user input and only contains an id. It also has a Set of friends with only ids. Here is the code:

 public User saveUserFriends(User user) {

     Transaction transaction = null;
        try {
            User retrievedUser = (User) session.get(User.class, user.getId());

            transaction = session.beginTransaction();

             for(User friend :user.getFriends()){
                System.out.println("friend "+friend.getId());
                friend = (User) session.get(User.class, friend.getId());
                System.out.println("retrieved user id "+user.getId());

                retrievedUser.getFriends().add(friend);

                session.save(retrievedUser);
                transaction.commit();
            }



        }catch (HibernateException e) {
            e.printStackTrace();
        } finally {
            session.close();
        }




    return user;
}

My hibernate mapping looks like this:

<hibernate-mapping>
    <class name="com.User" table="User">
        <id name="id" type="int">
        <column name="userId" />
            <generator class="native" />
        </id>
        <set name="friends" table="User_Friend" 
                inverse="true" lazy="false" cascade="none">
            <key column="userId"/>
            <many-to-many column="friendId" class="com.User" />
        </set>
        <set name="users" table="User_Friend" 
                inverse="true" lazy="false" cascade="none">
            <key column="friendId"/>
            <many-to-many column="userId" class="com.User" />
        </set>
    </class>
</hibernate-mapping>

The output is only selects. Why are the relationships not being created?

  • 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-06-16T00:07:54+00:00Added an answer on June 16, 2026 at 12:07 am

    I’m not a specialist of XML mapping, but both sides of the many-to-many associations are marked as the inverse side (inverse="true"). So Hibernate doesn’t care about them when persisting the user.

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

Sidebar

Related Questions

i am trying to persist two entities(with one to many relationship) using jpa but
I am trying to persist a one-to-many and a many-to-one relation using Hibernate 4.1.1
I am trying to persist a one (Owner) to many (Car) relation with morphia(0.99)/mongoDB(2).
I am currently trying to persist Joda Time in Hibernate using usertype , but
I'm using jqgrid on a page and I'm trying to persist column sorting between
I have a bidirectional one-to-many relationship. I'm trying to persist it like in this
I'm trying to persist an object into a MongoDB, using the following bit of
I'm trying to setup Spring using Hibernate and JPA, but when trying to persist
I'm trying to persist my object but when I look in the dev console,
I'm trying to persist user settings to a configuration file using ConfigurationManager. I want

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.