Skip to content

Commit

Permalink
Merge pull request #7527 from loicmathieu/feat/delete-by-id
Browse files Browse the repository at this point in the history
Panache : deleteById()
  • Loading branch information
FroMage authored Mar 23, 2020
2 parents 4d53262 + 65ca54c commit 82786db
Show file tree
Hide file tree
Showing 20 changed files with 323 additions and 38 deletions.
6 changes: 6 additions & 0 deletions docs/src/main/asciidoc/hibernate-orm-panache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ Person.delete("status", Status.Alive);
// delete all persons
Person.deleteAll();
// delete by id
boolean deleted = Person.deleteById(personId);
// update all living persons
Person.update("name = 'Moral' where status = ?1", Status.Alive);
Expand Down Expand Up @@ -379,6 +382,9 @@ personRepository.delete("status", Status.Alive);
// delete all persons
personRepository.deleteAll();
// delete by id
boolean deleted = personRepository.deleteById(personId);
// update all living persons
personRepository.update("name = 'Moral' where status = ?1", Status.Alive);
Expand Down
9 changes: 9 additions & 0 deletions docs/src/main/asciidoc/mongodb-panache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ Person.delete("status", Status.Alive);
// delete all persons
Person.deleteAll();
// delete by id
boolean deleted = Person.deleteById(personId);
----

All `list` methods have equivalent `stream` versions.
Expand Down Expand Up @@ -382,6 +385,9 @@ personRepository.delete("status", Status.Alive);
// delete all persons
personRepository.deleteAll();
// delete by id
boolean deleted = personRepository.deleteById(personId);
----

All `list` methods have equivalent `stream` versions.
Expand Down Expand Up @@ -800,6 +806,9 @@ Uni<Long> deleteCount = ReactivePerson.delete("status", Status.Alive);
// delete all persons
deleteCount = ReactivePerson.deleteAll();
// delete by id
Uni<Boolean> deleted = ReactivePerson.deleteById(personId);
----

TIP: If you use MongoDB with Panache in conjunction with RESTEasy, you can directly return a reactive type inside your JAX-RS resource endpoint as long as you include the `quarkus-resteasy-mutiny` extension.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,17 @@ public static long deleteAll() {
throw JpaOperations.implementationInjectionMissing();
}

/**
* Delete an entity of this type by ID.
*
* @param id the ID of the entity to delete.
* @return false if the entity was not deleted (not found).
*/
@GenerateBridge
public static boolean deleteById(Object id) {
throw JpaOperations.implementationInjectionMissing();
}

/**
* Delete all entities of this type matching the given query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,17 @@ public default long deleteAll() {
throw JpaOperations.implementationInjectionMissing();
}

/**
* Delete an entity of this type by ID.
*
* @param id the ID of the entity to delete.
* @return false if the entity was not deleted (not found).
*/
@GenerateBridge
public default boolean deleteById(Id id) {
throw JpaOperations.implementationInjectionMissing();
}

/**
* Delete all entities of this type matching the given query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,17 @@ public static long deleteAll(Class<?> entityClass) {
return (long) getEntityManager().createQuery("DELETE FROM " + getEntityName(entityClass)).executeUpdate();
}

public static boolean deleteById(Class<?> entityClass, Object id) {
// Impl note : we load the entity then delete it because it's the only implementation generic enough for any model,
// and correct in all cases (composite key, graph of entities, ...). HQL cannot be directly used for these reasons.
Object entity = findById(entityClass, id);
if (entity == null) {
return false;
}
getEntityManager().remove(entity);
return true;
}

public static long delete(Class<?> entityClass, String query, Object... params) {
return bindParameters(getEntityManager().createQuery(createDeleteQuery(entityClass, query, paramCount(params))), params)
.executeUpdate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,17 @@ public static long deleteAll() {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Delete an entity of this type by ID.
*
* @param id the ID of the entity to delete.
* @return false if the entity was not deleted (not found).
*/
@GenerateBridge
public static boolean deleteById(Object id) {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Delete all entities of this type matching the given query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,17 @@ public default long deleteAll() {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Delete an entity of this type by ID.
*
* @param id the ID of the entity to delete.
* @return false if the entity was not deleted (not found).
*/
@GenerateBridge
public default boolean deleteById(Id id) {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Delete all entities of this type matching the given query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bson.Document;

import io.quarkus.mongodb.panache.reactive.runtime.ReactiveMongoOperations;
import io.quarkus.mongodb.panache.runtime.MongoOperations;
import io.quarkus.mongodb.reactive.ReactiveMongoCollection;
import io.quarkus.mongodb.reactive.ReactiveMongoDatabase;
import io.quarkus.panache.common.Parameters;
Expand Down Expand Up @@ -707,6 +708,17 @@ public static Uni<Long> deleteAll() {
throw ReactiveMongoOperations.implementationInjectionMissing();
}

/**
* Delete an entity of this type by ID.
*
* @param id the ID of the entity to delete.
* @return false if the entity was not deleted (not found).
*/
@GenerateBridge
public static Uni<Boolean> deleteById(Object id) {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Delete all entities of this type matching the given query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bson.Document;

import io.quarkus.mongodb.panache.reactive.runtime.ReactiveMongoOperations;
import io.quarkus.mongodb.panache.runtime.MongoOperations;
import io.quarkus.mongodb.reactive.ReactiveMongoCollection;
import io.quarkus.mongodb.reactive.ReactiveMongoDatabase;
import io.quarkus.panache.common.Parameters;
Expand Down Expand Up @@ -703,6 +704,17 @@ public default Uni<Long> deleteAll() {
throw ReactiveMongoOperations.implementationInjectionMissing();
}

/**
* Delete an entity of this type by ID.
*
* @param id the ID of the entity to delete.
* @return false if the entity was not deleted (not found).
*/
@GenerateBridge
public default Uni<Boolean> deleteById(Id id) {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Delete all entities of this type matching the given query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,12 @@ public static Uni<Long> deleteAll(Class<?> entityClass) {
return collection.deleteMany(new Document()).map(deleteResult -> deleteResult.getDeletedCount());
}

public static Uni<Boolean> deleteById(Class<?> entityClass, Object id) {
ReactiveMongoCollection<?> collection = mongoCollection(entityClass);
Document query = new Document().append(ID, id);
return collection.deleteOne(query).map(results -> results.getDeletedCount() == 1);
}

public static Uni<Long> delete(Class<?> entityClass, String query, Object... params) {
String bindQuery = bindQuery(entityClass, query, params);
Document docQuery = Document.parse(bindQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.mongodb.client.model.ReplaceOptions;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.WriteModel;
import com.mongodb.client.result.DeleteResult;

import io.quarkus.arc.Arc;
import io.quarkus.mongodb.panache.MongoEntity;
Expand Down Expand Up @@ -538,6 +539,13 @@ public static long deleteAll(Class<?> entityClass) {
return collection.deleteMany(new Document()).getDeletedCount();
}

public static boolean deleteById(Class<?> entityClass, Object id) {
MongoCollection collection = mongoCollection(entityClass);
Document query = new Document().append(ID, id);
DeleteResult results = collection.deleteOne(query);
return results.getDeletedCount() == 1;
}

public static long delete(Class<?> entityClass, String query, Object... params) {
String bindQuery = bindQuery(entityClass, query, params);
Document docQuery = Document.parse(bindQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,35 +707,32 @@ private void generateDeleteById(ClassCreator classCreator, FieldDescriptor entit
ResultHandle entityClass = deleteById.readInstanceField(entityClassFieldDescriptor,
deleteById.getThis());

ResultHandle entity = deleteById.invokeStaticMethod(
MethodDescriptor.ofMethod(JpaOperations.class, "findById", Object.class, Class.class,
ResultHandle deleted = deleteById.invokeStaticMethod(
MethodDescriptor.ofMethod(JpaOperations.class, "deleteById", boolean.class, Class.class,
Object.class),
entityClass, id);

BranchResult entityNullBranch = deleteById.ifNull(entity);
BytecodeCreator entityNull = entityNullBranch.trueBranch();
BranchResult deletedBranch = deleteById.ifNonZero(deleted);
BytecodeCreator deletedFalse = deletedBranch.falseBranch();

ResultHandle idToString = entityNull.invokeVirtualMethod(
ResultHandle idToString = deletedFalse.invokeVirtualMethod(
ofMethod(Object.class, "toString", String.class),
id);
ResultHandle formatArgsArray = entityNull.newArray(Object.class, 1);
entityNull.writeArrayValue(formatArgsArray, entityNull.load(0), idToString);
ResultHandle formatArgsArray = deletedFalse.newArray(Object.class, 1);
deletedFalse.writeArrayValue(formatArgsArray, deletedFalse.load(0), idToString);

ResultHandle messageFormat = entityNull.load("No entity " + entityTypeStr + " with id %s exists");
ResultHandle message = entityNull.invokeStaticMethod(
ResultHandle messageFormat = deletedFalse.load("No entity " + entityTypeStr + " with id %s exists");
ResultHandle message = deletedFalse.invokeStaticMethod(
MethodDescriptor.ofMethod(String.class, "format", String.class, String.class, Object[].class),
messageFormat, formatArgsArray);

ResultHandle exception = entityNull.newInstance(
ResultHandle exception = deletedFalse.newInstance(
MethodDescriptor.ofConstructor(IllegalArgumentException.class, String.class),
message);
entityNull.throwException(exception);
deletedFalse.throwException(exception);
deletedFalse.breakScope();

BytecodeCreator entityNotNull = entityNullBranch.falseBranch();
entityNotNull.invokeStaticMethod(
MethodDescriptor.ofMethod(JpaOperations.class, "delete", void.class, Object.class),
entity);
entityNotNull.returnValue(null);
deleteById.returnValue(null);
}
try (MethodCreator bridgeDeleteById = classCreator.getMethodCreator(bridgeDeleteByIdDescriptor)) {
MethodDescriptor deleteById = MethodDescriptor.ofMethod(generatedClassName, "deleteById",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.quarkus.it.panache;

import java.io.Serializable;
import java.util.Objects;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;

import io.quarkus.hibernate.orm.panache.PanacheEntityBase;

@Entity
@IdClass(ObjectWithCompositeId.ObjectKey.class)
public class ObjectWithCompositeId extends PanacheEntityBase {
@Id
public String part1;
@Id
public String part2;
public String description;

static class ObjectKey implements Serializable {
private String part1;
private String part2;

public ObjectKey() {
}

public ObjectKey(String part1, String part2) {
this.part1 = part1;
this.part2 = part2;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
ObjectKey objectKey = (ObjectKey) o;
return part1.equals(objectKey.part1) &&
part2.equals(objectKey.part2);
}

@Override
public int hashCode() {
return Objects.hash(part1, part2);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.quarkus.it.panache;

import java.io.Serializable;
import java.util.Objects;

import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;

import io.quarkus.hibernate.orm.panache.PanacheEntityBase;

@Entity
public class ObjectWithEmbeddableId extends PanacheEntityBase {
@EmbeddedId
public ObjectKey key;
public String description;

@Embeddable
static class ObjectKey implements Serializable {
private String part1;
private String part2;

public ObjectKey() {
}

public ObjectKey(String part1, String part2) {
this.part1 = part1;
this.part2 = part2;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
ObjectKey objectKey = (ObjectKey) o;
return part1.equals(objectKey.part1) &&
part2.equals(objectKey.part2);
}

@Override
public int hashCode() {
return Objects.hash(part1, part2);
}
}
}
Loading

0 comments on commit 82786db

Please sign in to comment.