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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:22:29+00:00 2026-05-29T08:22:29+00:00

This has bugged me for years but I cannot find anything about it in

  • 0

This has bugged me for years but I cannot find anything about it in jpa references nor this site. When an entity has a computed member (Inverse via mappedby, @Formula, …), there doesn’t seem to be any way to get a transactionally consistent update of that computed member. The pattern seems to be that the setter of the one side needs to know about the consumers and update them in situ. I must be missing something. Hopefully the below is illustrative enough.

I wouldn’t mind if the requirement was that I had to call em.getReference or .find again on the obj w/ the computed members just in case the ones from the original fetch are out of sync, but that just returns the exact same instance w/ no updates of the fields.

import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

@Entity
public class InverseProblemDto {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @OneToMany(mappedBy="problem")
    public Set<OwnerDto> owners;

    public int otherField = 0;
}

@Entity
public class OwnerDto {
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    @ManyToOne
    public InverseProblemDto problem; 

    public int yetAnotherField = 0;
}


@Transactional
public void wontWork(int dbId) {
    @PersistenceContext
    EntityManager em;

    InverseProblemDto probDto = em.find(InverseProblemDto.class, dbId);
    probDto.otherField++;

    for (OwnerDto other : probDto.owners) {
        // do something
    }

    OwnerDto newOwner = new OwnerDto();
    newOwner.problem = probDto;
    em.persist(newOwner);

    // do more

    // How to ensure this finds newOwner w/o breaking transactional integrity by forcing a flush and refresh???
    for (OwnerDto other : probDto.owners) {
        if (other.id == newOwner.id) System.out.println("Yeah! found 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-29T08:22:30+00:00Added an answer on May 29, 2026 at 8:22 am

    Flush and refresh do not commit the transaction but only issue the pending sql to the database.

    You could always change the value yourself like this :

    @Entity
    public class InverseProblemDto {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int id;
        @OneToMany(mappedBy="problem")
        public Set<OwnerDto> owners;
    
        public int otherField = 0;
    
        public void addOwner(OwnerDto owner) {
            ...
        }
    }
    
    @Entity
        public class OwnerDto {
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id")
        private int id;
    
        @ManyToOne
        public InverseProblemDto problem; 
    
        public int yetAnotherField = 0;
    
        public void setProblem(InverseProblemDto problem) {
            if(problem != null) {
                problem.addOwners(this);
            }
            this.problem = problem;
        }
    

    }

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

Sidebar

Related Questions

This has probably been asked, but I can't find it... I want to link
This problem has bugged me for years, and I always feel like I'm coming
This has bugged me for a long time. 99% of the time, the GROUP
This has always bugged me. Perhaps someone with some hardcore knowledge of .NET internals
This has always bugged me to what is the best way to do the
This problem has bugged me so many times and i have now decided to
This has got to be something I just missed, but how do I add
This has been troubling me for a few years, and was recently exacerbated by
One of the things that has long bugged me about the FileSystemWatcher is the
This has always bugged the hell out of me. Why are lists like this?

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.