Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jpa - WildFly/Hibernate: update an embedded property failed

Currently, I am trying to update the eclipse-ee4j/cargotracker(originally it is a rewritten version of the Eric's DDD book sample) to the latest Jakarta EE 8:

https://github.com/hantsy/cargotracker/tree/jakartaee8

Personally, I want to expand WildFly as an alternative application server. But I encountered several wired issues between Payara/ElipseLInks and WildFly/Hibernate.

There is a Cargo entity that has a RouteSepcificaiton embeddable property that has departure and arrival Location(Entity) and arrival deadline date.

Dummy code fragments of the relations:

@Entity class Cargo{
    @Embedded routeSepcification;
}

@Embedable class RouteSpecificition{
    @ManyToOne Location origin
    @ManyToOne Location destination
}
@Entity class Location{}

The book new cargo works well, but when I want to change to the new route using cargo.specifyNewRoute, in another word, to update the route specification, it is not updated as expected.

The complete test code is here.

startTransaction();
        var trackingId = new TrackingId("AAA");
        Cargo cargo = cargoRepository.find(trackingId);

        assertThat(cargo).isNotNull();

        Location origin = locationRepository.find(SampleLocations.NEWYORK.getUnLocode());
        Location destination = locationRepository.find(SampleLocations.HELSINKI.getUnLocode());

        cargo.specifyNewRoute(new RouteSpecification(origin, destination, LocalDate.now()));

        cargoRepository.store(cargo);
        commitTransaction();

        //verify in the new tx
        startTransaction();
        var result = this.entityManager.createQuery("select c from Cargo c where c.trackingId=:trackingId", Cargo.class)
                .setParameter("trackingId", trackingId)
                .getSingleResult();

        assertThat(result.getTrackingId()).isEqualTo(trackingId);
        assertThat(result.getRouteSpecification().getOrigin()).isEqualTo(origin);
        assertThat(result.getRouteSpecification().getDestination()).isEqualTo(destination);
        assertThat(result.getItinerary().getLegs()).hasSize(1);

        commitTransaction();

The Arquillian test is failed on WildFly/Hibernate but passed on Payara/EclipseLinks.

question from:https://stackoverflow.com/questions/65839254/wildfly-hibernate-update-an-embedded-property-failed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

have you implemented the equals method for the Location entity?

If not I think the tests fails because the EntityManager used to retrieve the origin and destination and then to update the Cargo is not the same used in the second transaction block when you try to retrieve the updated Cargo, for this reason two new instances of the destination and origin Location are created by the second EntityManager.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...