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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:28:26+00:00 2026-05-12T09:28:26+00:00

I have two entities like the following: @Entity public class Trip { @OneToMany(mappedBy =

  • 0

I have two entities like the following:

@Entity
public class Trip {

    @OneToMany(mappedBy = "trip", fetch = FetchType.LAZY)
    private Set<Job> jobs = new HashSet<Job>();

}

@Entity
public class Job {

    @ManyToOne(fetch = FetchType.LAZY)
    private Trip trip;

}

The problem is that the mappedBy relationship behaves differently in different circumstances. Here is an example

    EntityManager em1 = unit.createEntityManager();
    EntityManager em2 = unit.createEntityManager();

    // One EM starts transaction and stores a trip
    em1.getTransaction().begin();

    Trip trip = new Trip();
    em1.persist(trip);

    Long tripId = trip.getId();

    assertThat(tripId).isPositive();

    em1.getTransaction().commit();

    // Then em2 starts different transaction
    em2.getTransaction().begin();

    // Looking up for the trip through clean em (cache is empty)
    Trip em2Trip = em2.find(Trip.class, tripId);

    Job job = new Job();
    job.setTrip(em2Trip);

    em2.persist(job);

    // The em2Trip should not be updated

    assertThat(em2Trip.getJobs()).hasSize(1);

    em2.getTransaction().commit();

    em1.getTransaction().begin();

    Trip em1Trip = em1.find(Trip.class, tripId);

    // fails here
    assertThat(em1Trip.getJobs()).hasSize(1);

    em1.getTransaction().commit();

The code above shows that if an entity is already loaded in the entity manager’s cache, the getter for the mappedBy relationship may return invalid results.

I have a proof it doesn’t work under JBoss either. The following code behaves differently depending on which entity manager is being used. The result is unpredictable.

        Trip trip = em.find(Trip.class, tripId);
        if (trip.getJobs().size() == 0) ...

Does this mean that the mappedBy automatically makes application buggy as soon as it is introduced and used?

P.S. I am not trying to abuse hibernate. I only want to find out if someone faced such a problem and how did they cope with it

  • 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-12T09:28:27+00:00Added an answer on May 12, 2026 at 9:28 am

    Behavior you’re describing has absolutely nothing to do with associations; you would get the exact same results if you just tried to read / update simple POJO from two entity managers. Once your entity is associated with persistence context it will NOT be automatically refreshed from the database. This is a documented behavior – and in the vast majority of cases this is a DESIRED behavior.

    As far as “keeping your application stable” goes:

    1. EntityManager instances directly correspond to Hibernate sessions and thus should not be held for prolonged periods of time. If you rewrite your code above to use a new EntityManager instance (em3) instead of reusing em1, your problem will go away.
    2. EntityManager has a refresh() method which you can invoke to reload entity state from the database.
    3. EntityManager has a clear() method which will clear the persistence context completely thus preventing this issue as well. Use it sparingly and cautiously, though – invoking clear() without flush() will throw away all pending updates.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have the following two entities with many-to-many association: @Entity public class Role {
I have two entities like this: public class Service { public virtual int ServiceId
I have two entities A and B. public class A{ @Id @GeneratedValue private Integer
A have two entities. For example timing settings and orders. @Entity public class TimingSettings{
I have the following two entities: public class Entity1 { public IList e2 {
I have two entities: ResourceFile: @Entity @Table(name = resource_file) public class ResourceFile extends IdEntity<Integer>
I have the following entities set up in Code First Entity Framework: public class
I have the following two entities: public class Field { public int FieldID {
I have two entities: Recipe and Ingredient. Entites: public class Ingredient { public int
I have two entities: public class Parent() { public ICollection<Child> Children { get; set;

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.