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 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 memory but can't find answers with Google. public class
I'm trying to refresh update panel via Javascript: __doPostBack(<%=upMyPanel.ClientID %>, ); But somehow its
I am trying to create a user interface, but when I refresh the page,
I'm trying to refresh a classic report with this code and it works fine
I am putting some ajax into a rails project. I am trying to refresh
I'm trying to force PreferenceActivity to refresh. Does anyone know how to do this?
i'm trying to apply the refresh trigger in my coffeescript code, but I don't
I am trying to refresh a page, but WANT to use the cache and
I'm trying to refresh my main view in this code. It shows the right
Im trying to execute a refresh on a materialized view, but I cant get

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.