Welcome to OGeek Q&A 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...