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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:22:31+00:00 2026-06-06T20:22:31+00:00

First of all, this solution is no option for me, because I can’t change

  • 0

First of all, this solution is no option for me, because I can’t change the persistence-unit.

My problem is that I use a JTA EntityManager but I need for exactly one use case something like a transaction:

public boolean saveWithResult(PointsValidityPeriod pointsValidityPeriod)
{
    //TODO use transaction here 
    super.save(pointsValidityPeriod);

    if (updatePrevious(pointsValidityPeriod.getValidFrom()) != 1)
    {
        logger.error("Update of Period was not possible, because UPDATE returned no single result.");

        return false;
    }

    pointsValidityPeriodEvent.fire(pointsValidityPeriod);

    return true;
}

Save method (which I can’t change):

public void save(T entity)
{
    getEntityManager().persist(entity);
}

You see, that there is a save invocation, but this save must be rolled back if the update went wrong, so how can I achieve that? Any ideas?

  • 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-06T20:22:33+00:00Added an answer on June 6, 2026 at 8:22 pm

    I have the solution but I don’t know why this works. Maybe someone could explain it to me.
    In my backing bean there is the save method. There are 2 entities. getEntity() is the current entity used by the backing bean, currentValidityPeriod is another instance of that Entity which holds the latest database state of the entity, fetched by currentValidityPeriod = pointsValidityService.findCurrent(); findCurrent() is just a method which executes a TypedQuery:

    public PointsValidityPeriod findCurrent()
    {
        TypedQuery<PointsValidityPeriod> validityPeriodQuery = entityManager.createNamedQuery(PointsValidityPeriod.FindCurrent, PointsValidityPeriod.class);
    
        return validityPeriodQuery.getSingleResult();
    }
    

    This is the save method invoked by the view. As you can see, I only pass the current Entity to the service save method. In the previous Entity (currentValidityPeriod), I set another value.

    @Override
    public void save()
    {       
        Date validFrom = new DateTime(DateTimeZone.forID("Europe/Vienna")).toDate();
        getEntity().setValidFrom(validFrom);
    
        currentValidityPeriod.setValidThru(validFrom);
    
        pointsValidityService.save(getEntity());
    
        addSavedSuccessfullyMessage();
    }
    

    This is the service method which will be invoked:

    @Override
    public void save(PointsValidityPeriod pointsValidityPeriod)
    {
        super.save(pointsValidityPeriod);
    }
    

    …and Superclass’s save method:

    public void save(T entity)
    {
        getEntityManager().persist(entity);
    }
    

    Why does the Entity Manager persist the new Entity and update the old one? I just pass ONE Entity to the Entity Manager. Although this is the expected behavior, I’d like to know why this works.

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

Sidebar

Related Questions

EDIT: solved, look below for my solution. first of all, this is my very
First of all: This question is not directly programming related. However, the problem only
First of all this is my markup: <div class=first></div> <div class=second></div> <div class=second></div> <div
first of all: this is not the same as this . The ModelBackend has
first of all this is my third question about web services here and i
Well, first of all sorry about this question it must be pretty straight forward
Maybe it's a similar beginning, but it's true. first of all sorry if this
First of all there is probably a question like this already but i couldn't
First of all I'm not sure this is even possible, however I need to
I'm trying to use System.Windows.Forms.HTMLDocument in a console application. First, is this even possible?

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.