Quarkus: Provide a more generic save method in Hibernate ORM Panache

Created on 11 Sep 2019  路  13Comments  路  Source: quarkusio/quarkus

Currently the persist is provided, I can not found the merge method.

If possible provide a generic save method(and saveAll) like Spring Data JPA.

if( Entity is new) {
     em.persist(entity)
     return entity;
}else{
     return em.merge(entity);
}
arepanache kinenhancement

All 13 comments

It's strange that you write that because the merge method is implemented by the EntityManager and up til now I have been using it without any problem.
This is a piece of one of my codes and I am using Hibernate with Panache

    public Response.ResponseBuilder update(final UpdateTourRequest request) throws ValidationException {
        Response.ResponseBuilder responseBuilder;
        Tour tour = findTour(request.getCode());
        tour.transportation = "";
        em.merge(tour);
        responseBuilder = ok(new UpdateTourResponse(request.getCorrelationId(), objectFactory.createCommonTour(tour)));
        LOGGER.info("Tour with code {} --> {}", request.getCode(), tour);
        return responseBuilder;
    }

@dwamara I have not found merge in the PanacheEntity and PanacheRepository.

@hantsy because you can find it in the EntityManager. The datasource you declare in your application.properties also serves to instantiate the EntityManager and you can then use it to merge your entities if you want.

@dwamara Yes, as a workaround, it worked well. I understood your meaning. What I said is it is missing in the Quarkus's Hibernate ORM Panache.

@dwamara I do not get your explanation about

because you can find it in the EntityManager.

You can also find the method persist in the entity manager and even though it is implemented in the PanacheEntityBase. Would you mind to explain me, why merge is not implemented yet? I mean no offense, just curious.

This issue/pullrequest has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@hantsy for MongoDB with Panache we provide entity.persistOrUpdate() that save the entity if not existing in the database otherwise update it.

Will the same functionality on Hibernate with Panache OK for you ?

Some pseudo code implementation would be :

public void persistOrUpdate() {
    if(entity.isPersistent()){
        entity.persist();
    }
    else {
        em.merge(entity);
    }
}

Warning that as isPersistent uses EntityManager.contains() it will not works if the entity is not yet managed by the entity manager but already existant in the database.

@loicmathieu if Mongo used saveOrUpdate, I think use the same naming is better.

@loicmathieu if Mongo used saveOrUpdate, I think use the same naming is better.

This was an error of mine, I updated my comment, MongoDB with Panache uses persistOrUpdate() so we should use the same here.

@dwamara To make an update on an entity inside Hibernate with Panache you can load the entity from the database (via findById() for exemple) then update it's fields. There is no need to use merge from the Entity manager as all managed entity will have there modifications sent to the database automatically (this is standard JPA behaviour).

Anyway, I think we should ask @FroMage about this he's the Hiberate with Panache expert ;)

Yeah, this is code built around unserialising the entity itself rather than a DTO which is a very dangerous way to build applications with many security risks that we don't want to promote.

. There is no need to use merge from the Entity manager as all managed entity will have there modifications sent to the database automatically (this is standard JPA behaviour).

This depends on the session/transaction boundary. If the modifying entities happen in an active session it works. @Transcational must ensure there is an active session available and commit the transaction when closing the session.

@FroMage WDYT of this one ?

Should we provides persistOrUpdate for Hibernate with Panache with an implementation close to what I propose here: https://github.com/quarkusio/quarkus/issues/3969#issuecomment-570586080 or should we close it ?

Maybe @Sanne has some ideas around this as well ;)

Well, we're still trying to avoid exposing merge at this point, so let's hold.

Was this page helpful?
0 / 5 - 0 ratings