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

  • Home
  • SEARCH
  • 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 774633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:09:46+00:00 2026-05-14T19:09:46+00:00

I am trying to refresh the @ManyToMany relation but it gets cleared instead… My

  • 0

I am trying to refresh the @ManyToMany relation but it gets cleared instead…

My Project class looks like this:

@Entity
public class Project {
    ...
    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "PROJECT_USER",
    joinColumns = @JoinColumn(name = "PROJECT_ID", referencedColumnName = "ID"),
    inverseJoinColumns = @JoinColumn(name = "USER_ID", referencedColumnName = "ID"))
    private Collection<User> users;
    ...
}

But I don’t have – and I don’t want – the collection of Projects in the User entity.

When I look at the generated database tables, they look good. They contain all columns and constraints (primary/foreign keys).

But when I persist a Project that has a list of Users (and the users are still in the database), the mapping table doesn’t get updated gets updated but when I refresh the project afterwards, the list of Users is cleared.

For better understanding:

Project project = ...; // new project with users that are available in the db
System.out.println(project getUsers().size()); // prints 5
em.persist(project);
System.out.println(project getUsers().size()); // prints 5
em.refresh(project);
System.out.println(project getUsers().size()); // prints 0

So, how can I refresh the relation between User and Project?

  • 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-14T19:09:47+00:00Added an answer on May 14, 2026 at 7:09 pm

    The error seems to be in the code that you left out with …

    I am assuming that project actually did come out of the database (making persist a noop), otherwise you should be getting a hibernate exception when you try to refresh an entity that hasn’t been committed yet.

    There seems to just be some confusion here about the difference between JDBC and ORM and what an EntityManager actually does. em.persist is not a SQL insert or a SQL update. It says ‘take this new entity and put it in the managed state.’ Usually that eventually leads to a SQL insert, but that’s not what it means at the application level.

    It looks like you’re essentially telling hibernate “I made some changes to this, next time the session is synchronized please persist them, oh, you know what, I changed my mind, revert back to what’s in the database.” em.persist does not directly put anything in the database, what’s there is probably still “nothing”.

    Try tossing an em.flush in before your refresh and you should see your Users.

    (You haven’t said anything about how your session is configure or how you’re managing transactions, so I’m assuming all of this is in one transaction.)

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

Sidebar

Related Questions

I'm trying to refresh my recent list every 5 seconds. I was looking at
I am trying to refresh a certain part of my page with AJAX and
While I was trying to refresh page contents dynamically using Ajax/JQuery, I have learned
I'm trying to implement returning from an Activity without needing to refresh the screen.
I am trying to get a winform app to refresh an embedded browser on
I have a table with a MVIEW Log, i would like to know if
I am getting the Expected ':' before '.' token error with this piece of
I'm trying to display some text in a div named msg_box for each selected
I'm trying to write an iPhone app that takes PNG tilesets and displays segments
My first forray into ajax webpages is causing me some problems. My basic structure

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.