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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:28:18+00:00 2026-06-12T02:28:18+00:00

In my current project I have an entity which can be published to other

  • 0

In my current project I have an entity which can be published to other systems. For keeping track on the publications the entity has a relation called “publications”. I am using Eclipselink.

This entity bean also has a “PreUpdate” annotated method.

In order to be able to keep the other systems data up to date, I created an Aspect that is executed around the call to the PreUpdate method. Depending on which properties have changed, I need to remove some of the publications. Everything is working absolutely fine.

The problem I am having is that the portal-publishing component correctly sends delete commands and removes the publication from the entities “publications” list. I can even see in the changeset that JPA has noticed the “publications” property to have changed. After the transaction is flushed, the cached entity correctly doesn’t have the deleted publications anymore. Unfortunately the database still does and when the system is restarted or the Entity is loaded from the DB again, the publication metadata is there again.

I tried allmost everything. I even managed to get the deleted instances from the JPA ChangeSet in the Aspect and tried to use the entityManager to manually delete them, but nothing actually worked. I seem to be unable to delete these relational entities. Currently I am thinking about using JDBC to delete them, but this would only be my last measure.

@Transactional
@Around("execution(* de.cware.services.truck.model.Truck.jpaPreUpdate(..))")
public Object truckPreUpdate(final ProceedingJoinPoint pjp) throws Throwable {
    if (alreadyExecutingMarker.get() != Boolean.TRUE) {
        alreadyExecutingMarker.set(Boolean.TRUE);

        final Truck truck = (Truck) pjp.getTarget();

        final JpaEntityManager jpaEntityManager = (JpaEntityManager) entityManager.getDelegate();
        final UnitOfWorkChangeSet changeSet = jpaEntityManager.getUnitOfWork().getCurrentChanges();
        final ObjectChangeSet objectChangeSet = changeSet.getObjectChangeSetForClone(truck);

        if (log.isDebugEnabled()) {
            log.debug("--------------------- Truck pre update check (" + truck.getId() + ") ---------------------");
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // If the truck date has changed, revoke all publication copies.
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////

        final ChangeRecord truckFreeDate = objectChangeSet.getChangesForAttributeNamed("lkwFreiDatum");
        if (truckFreeDate != null) {
            if (log.isDebugEnabled()) {
                log.debug("The date 'truckFreeDate' of truck with id '" + truck.getId() + "' has changed. " +
                        "Revoking all publications that are not marked as main applications");
            }

            for (final String portal : truck.getPublishedPortals()) {
                if (log.isDebugEnabled()) {
                    log.debug("- Revoking publications of copies to portal: " + portal);
                }

                portalService.deleteCopies(truck, portal);

                // Get any deleted portal references and use the entityManager to finally delete them.
                changeSet = jpaEntityManager.getUnitOfWork().getCurrentChanges();
                objectChangeSet = changeSet.getObjectChangeSetForClone(truck);
                final ChangeRecord publicationChanges = objectChangeSet.getChangesForAttributeNamed("publications");
                if (publicationChanges != null) {
                    if (publicationChanges instanceof CollectionChangeRecord) {
                        final CollectionChangeRecord collectionChanges =
                                (CollectionChangeRecord) publicationChanges;
                        @SuppressWarnings("unchecked")
                        final Collection<ObjectChangeSet> removedPublications =
                                (Collection<ObjectChangeSet>)
                                        collectionChanges.getRemoveObjectList().values();
                        for (final ObjectChangeSet removedPublication : removedPublications) {
                            final TruckPublication publication = (TruckPublication) ((org.eclipse.persistence.internal.sessions.ObjectChangeSet) removedPublication).getUnitOfWorkClone();
                            entityManager.remove(publication);
                        }
                    }
                }
            }
        }
    }
}

Chris

  • 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-12T02:28:19+00:00Added an answer on June 12, 2026 at 2:28 am

    The only solution I found was to create a new Method for performing the delete and forcing JPA to create a new Transaction. As by this I am losing the changeSet, I had to manually find out which publications were deleted. I then simply call that helper method and the publications are correctly deleted, but I find this solution extremely ugly.

    @Transactional
    @Around(“execution(* de.cware.services.truck.model.Truck.jpaPreUpdate(..))”)
    public Object truckPreUpdate(final ProceedingJoinPoint pjp) throws Throwable {
    if (alreadyExecutingMarker.get() != Boolean.TRUE) {
    alreadyExecutingMarker.set(Boolean.TRUE);

        final Truck truck = (Truck) pjp.getTarget();
    
        final JpaEntityManager jpaEntityManager = (JpaEntityManager) entityManager.getDelegate();
        final UnitOfWorkChangeSet changeSet = jpaEntityManager.getUnitOfWork().getCurrentChanges();
        final ObjectChangeSet objectChangeSet = changeSet.getObjectChangeSetForClone(truck);
    
        if (log.isDebugEnabled()) {
            log.debug("--------------------- Truck pre update check (" + truck.getId() + ") ---------------------");
        }
    
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // If the truck date has changed, revoke all publication copies.
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
        final ChangeRecord truckFreeDate = objectChangeSet.getChangesForAttributeNamed("lkwFreiDatum");
        if (truckFreeDate != null) {
            if (log.isDebugEnabled()) {
                log.debug("The date 'truckFreeDate' of truck with id '" + truck.getId() + "' has changed. " +
                        "Revoking all publications that are not marked as main applications");
            }
    
            removeCopyPublications(truck);
        }
    }
    
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    protected void removeCopyPublications(Truck truck) {
        // Delete all not-main-publications.
        for (final String portal : truck.getPublishedPortals()) {
            if (log.isDebugEnabled()) {
                log.debug("- Revoking publications of copies to portal: " + portal);
            }
    
            final Map<Integer, TruckPublication> oldPublications = new HashMap<Integer, TruckPublication>();
            for(final TruckPublication publication : truck.getPublications(portal)) {
                oldPublications.put(publication.getId(), publication);
            }
    
            portalService.deleteCopies(truck, portal);
    
            for(final TruckPublication publication : truck.getPublications(portal)) {
                oldPublications.remove(publication.getId());
            }
    
            for (TruckPublication removedPublication : oldPublications.values()) {
                if(!entityManager.contains(removedPublication)) {
                    removedPublication = entityManager.merge(removedPublication);
                }
                entityManager.remove(removedPublication);
                entityManager.flush();
            }
        }
    }
    

    Why doesn’t my first version work?

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

Sidebar

Related Questions

I have a POJO class, say Foo, which has a Set of other entity
On my current project, which is a delivery system, I have a list of
For a current MVC3 project I have a model that has multiple pages for
my current project is based on Entity Framwork code-first. I have three types: Task,
In my current project I have to decide which technique to use when branching.
In my current project (which is really small) i have 3 tables/ POCO entities
For my current project I have to send a signature from PHP to Java
In my current project I have data about colors. Each color is either a
In my current project we have a large repository of content that was originally
I have a few classes in my current project where validation of Email/Website addresses

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.