Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/src/main/asciidoc/hibernate-orm-panache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ List<Person> allPersons = Person.listAll();
// finding a specific person by ID
person = Person.findById(personId);

// finding a specific person by ID via an Optional
Optional<Person> optional = Person.findByIdOptional(personId);
person = optional.orElseThrow(() -> new NotFoundException());

// finding all living persons
List<Person> livingPersons = Person.list("status", Status.Alive);

Expand Down
4 changes: 4 additions & 0 deletions docs/src/main/asciidoc/mongodb-panache.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ List<Person> allPersons = Person.listAll();
// finding a specific person by ID
person = Person.findById(personId);

// finding a specific person by ID via an Optional
Optional<Person> optional = Person.findByIdOptional(personId);
person = optional.orElseThrow(() -> new NotFoundException());

// finding all living persons
List<Person> livingPersons = Person.list("status", Status.Alive);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import javax.json.bind.annotation.JsonbTransient;
Expand Down Expand Up @@ -115,6 +116,29 @@ public static <T extends PanacheEntityBase> T findById(Object id, LockModeType l
throw JpaOperations.implementationInjectionMissing();
}

/**
* Find an entity of this type by ID.
*
* @param id the ID of the entity to find.
* @return if found, an optional containing the entity, else <code>Optional.empty()</code>.
*/
@GenerateBridge
public static <T extends PanacheEntityBase> Optional<T> findByIdOptional(Object id) {
throw JpaOperations.implementationInjectionMissing();
}

/**
* Find an entity of this type by ID.
*
* @param id the ID of the entity to find.
* @param lockModeType the locking strategy to be used when retrieving the entity.
* @return if found, an optional containing the entity, else <code>Optional.empty()</code>.
*/
@GenerateBridge
public static <T extends PanacheEntityBase> Optional<T> findByIdOptional(Object id, LockModeType lockModeType) {
throw JpaOperations.implementationInjectionMissing();
}

/**
* Find entities using a query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.hibernate.orm.panache;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import javax.persistence.LockModeType;
Expand Down Expand Up @@ -163,6 +164,15 @@ public interface PanacheQuery<Entity> {
*/
public <T extends Entity> T firstResult();

/**
* Returns the first result of the current page index. This ignores the current page size to fetch
* a single result.
*
* @return if found, an optional containing the entity, else <code>Optional.empty()</code>.
* @see #singleResultOptional()
*/
public <T extends Entity> Optional<T> firstResultOptional();

/**
* Executes this query for the current page and return a single result.
*
Expand All @@ -172,4 +182,13 @@ public interface PanacheQuery<Entity> {
* @see #firstResult()
*/
public <T extends Entity> T singleResult();

/**
* Executes this query for the current page and return a single result.
*
* @return if found, an optional containing the entity, else <code>Optional.empty()</code>.
* @throws NonUniqueResultException if there are more than one result
* @see #firstResultOptional()
*/
public <T extends Entity> Optional<T> singleResultOptional();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import javax.persistence.LockModeType;
Expand Down Expand Up @@ -113,6 +114,28 @@ public default Entity findById(Id id, LockModeType lockModeType) {
throw JpaOperations.implementationInjectionMissing();
}

/**
* Find an entity of this type by ID.
*
* @param id the ID of the entity to find.
* @return if found, an optional containing the entity, else <code>Optional.empty()</code>.
*/
@GenerateBridge
public default Optional<Entity> findByIdOptional(Id id) {
throw JpaOperations.implementationInjectionMissing();
}

/**
* Find an entity of this type by ID.
*
* @param id the ID of the entity to find.
* @return if found, an optional containing the entity, else <code>Optional.empty()</code>.
*/
@GenerateBridge
public default Optional<Entity> findByIdOptional(Id id, LockModeType lockModeType) {
throw JpaOperations.implementationInjectionMissing();
}

/**
* Find entities using a query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Stream;

import javax.persistence.EntityManager;
Expand Down Expand Up @@ -201,6 +202,14 @@ public static Object findById(Class<?> entityClass, Object id, LockModeType lock
return getEntityManager().find(entityClass, id, lockModeType);
}

public static Optional<?> findByIdOptional(Class<?> entityClass, Object id) {
return Optional.ofNullable(findById(entityClass, id));
}

public static Optional<?> findByIdOptional(Class<?> entityClass, Object id, LockModeType lockModeType) {
return Optional.ofNullable(findById(entityClass, id, lockModeType));
}

public static PanacheQuery<?> find(Class<?> entityClass, String query, Object... params) {
return find(entityClass, query, null, params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

import io.quarkus.hibernate.orm.panache.PanacheQuery;
Expand Down Expand Up @@ -139,10 +141,27 @@ public <T extends Entity> T firstResult() {
return list.isEmpty() ? null : list.get(0);
}

@Override
public <T extends Entity> Optional<T> firstResultOptional() {
return Optional.ofNullable(firstResult());
}

@Override
@SuppressWarnings("unchecked")
public <T extends Entity> T singleResult() {
jpaQuery.setMaxResults(page.size);
return (T) jpaQuery.getSingleResult();
}

@Override
@SuppressWarnings("unchecked")
public <T extends Entity> Optional<T> singleResultOptional() {
jpaQuery.setMaxResults(2);
List<T> list = jpaQuery.getResultList();
if (list.size() == 2) {
throw new NonUniqueResultException();
}

return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import org.bson.Document;
Expand Down Expand Up @@ -85,6 +86,17 @@ public static <T extends PanacheMongoEntityBase> T findById(Object id) {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Find an entity of this type by ID.
*
* @param id the ID of the entity to find.
* @return if found, an optional containing the entity, else <code>Optional.empty()</code>.
*/
@GenerateBridge
public static <T extends PanacheMongoEntityBase> Optional<T> findByIdOptional(Object id) {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Find entities using a query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import org.bson.Document;
Expand Down Expand Up @@ -91,6 +92,17 @@ public default Entity findById(Id id) {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Find an entity of this type by ID.
*
* @param id the ID of the entity to find.
* @return if found, an optional containing the entity, else <code>Optional.empty()</code>.
*/
@GenerateBridge
public default Optional<Entity> findByIdOptional(Id id) {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Find entities using a query, with optional indexed parameters.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.mongodb.panache;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import io.quarkus.panache.common.Page;
Expand Down Expand Up @@ -147,11 +148,30 @@ public interface PanacheQuery<Entity> {
*/
public <T extends Entity> T firstResult();

/**
* Returns the first result of the current page index. This ignores the current page size to fetch
* a single result.
*
* @return if found, an optional containing the entity, else <code>Optional.empty()</code>.
* @see #singleResultOptional()
*/
public <T extends Entity> Optional<T> firstResultOptional();

/**
* Executes this query for the current page and return a single result.
*
* @return the single result (throws if there is not exactly one)
* @return the single result
* @throws PanacheQueryException if there is not exactly one result.
* @see #firstResult()
*/
public <T extends Entity> T singleResult();

/**
* Executes this query for the current page and return a single result.
*
* @return if found, an optional containing the entity, else <code>Optional.empty()</code>.
* @throws PanacheQueryException if there is more than one result.
* @see #firstResultOptional()
*/
public <T extends Entity> Optional<T> singleResultOptional();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -282,6 +283,10 @@ public static Object findById(Class<?> entityClass, Object id) {
return collection.find(new Document(ID, id)).first();
}

public static Optional findByIdOptional(Class<?> entityClass, Object id) {
return Optional.ofNullable(findById(entityClass, id));
}

public static PanacheQuery<?> find(Class<?> entityClass, String query, Object... params) {
return find(entityClass, query, null, params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import org.bson.Document;
Expand All @@ -12,6 +13,7 @@

import io.quarkus.mongodb.panache.PanacheQuery;
import io.quarkus.panache.common.Page;
import io.quarkus.panache.common.exception.PanacheQueryException;

public class PanacheQueryImpl<Entity> implements PanacheQuery<Entity> {
private MongoCollection collection;
Expand Down Expand Up @@ -133,14 +135,30 @@ public <T extends Entity> T firstResult() {
return list.isEmpty() ? null : list.get(0);
}

@Override
public <T extends Entity> Optional<T> firstResultOptional() {
return Optional.ofNullable(firstResult());
}

@Override
@SuppressWarnings("unchecked")
public <T extends Entity> T singleResult() {
List<T> list = list();
if (list.isEmpty() || list.size() > 1) {
throw new RuntimeException("There should be only one result");//TODO use proper exception
throw new PanacheQueryException("There should be only one result");
}

return list.get(0);
}

@Override
@SuppressWarnings("unchecked")
public <T extends Entity> Optional<T> singleResultOptional() {
List<T> list = list();
if (list.size() > 1) {
throw new PanacheQueryException("There should be no more than one result");
}

return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.panache.common.exception;

public class PanacheQueryException extends RuntimeException {
public PanacheQueryException(String s) {
super(s);
}
}
Loading