diff --git a/extensions/panache/hibernate-orm-panache/runtime/src/main/java/io/quarkus/hibernate/orm/panache/runtime/JpaOperations.java b/extensions/panache/hibernate-orm-panache/runtime/src/main/java/io/quarkus/hibernate/orm/panache/runtime/JpaOperations.java index 9bf6d3bbb52cbb..2f6a13392739af 100644 --- a/extensions/panache/hibernate-orm-panache/runtime/src/main/java/io/quarkus/hibernate/orm/panache/runtime/JpaOperations.java +++ b/extensions/panache/hibernate-orm-panache/runtime/src/main/java/io/quarkus/hibernate/orm/panache/runtime/JpaOperations.java @@ -13,6 +13,11 @@ import javax.transaction.SystemException; import javax.transaction.TransactionManager; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.metadata.ClassMetadata; +import org.hibernate.metamodel.spi.MetamodelImplementor; + import io.quarkus.arc.Arc; import io.quarkus.hibernate.orm.panache.PanacheQuery; import io.quarkus.panache.common.Parameters; @@ -394,13 +399,15 @@ public static long deleteAll(Class entityClass) { } public static boolean deleteById(Class entityClass, Object id) { - // TODO can be improved using a DELETE JPQL query - Object entity = findById(entityClass, id); - if (entity == null) { - return false; - } - getEntityManager().remove(entity); - return true; + EntityManager entityManager = getEntityManager(); + Session session = entityManager.unwrap(Session.class); + SessionFactory sessionFactory = session.getSessionFactory(); + ClassMetadata metadata = ((MetamodelImplementor) sessionFactory.getMetamodel()).entityPersister(entityClass) + .getClassMetadata(); + String idField = metadata.getIdentifierPropertyName(); + + long updated = delete(entityClass, idField, id); + return updated == 1; } public static long delete(Class entityClass, String query, Object... params) { diff --git a/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java b/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java index b23e9878c243bb..68a4e7a58119e0 100644 --- a/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java +++ b/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/TestEndpoint.java @@ -214,6 +214,13 @@ public String testModel() { testUpdate(); + //delete by id + Person toRemove = new Person(); + toRemove.name = "testDeleteById"; + toRemove.uniqueName = "testDeleteByIdUnique"; + toRemove.persist(); + Assertions.assertTrue(Person.deleteById(toRemove.id)); + // persistAndFlush Person person1 = new Person(); person1.name = "testFLush1"; @@ -229,13 +236,6 @@ public String testModel() { //this is expected } - //delete by id - Person toRemove = new Person(); - toRemove.name = "testDeleteById"; - toRemove.uniqueName = "testDeleteById"; - toRemove.persist(); - Assertions.assertTrue(Person.deleteById(toRemove.id)); - return "OK"; } @@ -652,6 +652,13 @@ public String testModelDao() { testUpdateDAO(); + //delete by id + Person toRemove = new Person(); + toRemove.name = "testDeleteById"; + toRemove.uniqueName = "testDeleteByIdUnique"; + personDao.persist(toRemove); + Assertions.assertTrue(personDao.deleteById(toRemove.id)); + //flush Person person1 = new Person(); person1.name = "testFlush1"; @@ -667,13 +674,6 @@ public String testModelDao() { //this is expected } - //delete by id - Person toRemove = new Person(); - toRemove.name = "testDeleteById"; - toRemove.uniqueName = "testDeleteById"; - personDao.persist(toRemove); - Assertions.assertTrue(personDao.deleteById(toRemove.id)); - return "OK"; }