Skip to content

Commit

Permalink
fix: deleteById using a Query
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Mar 6, 2020
1 parent 3424c4f commit cde8244
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
}

Expand Down Expand Up @@ -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";
Expand All @@ -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";
}

Expand Down

0 comments on commit cde8244

Please sign in to comment.