query

@Suspendable
abstract fun <T : Any> query(queryName: String, entityClass: Class<T>): ParameterisedQuery<T>

Creates a ParameterisedQuery to support a named query to return a list of entities of the given type in a single transaction. Casts result set to the specified type T.

Example usage:

  • Kotlin:

// For JPA Entity:
@Suppress("Unused")
@CordaSerializable
@Entity
@Table(name = "DOGS")
@NamedQuery(name = "find_by_name_and_age", query = "SELECT d FROM Dog d WHERE d.name = :name AND d.age <= :maxAge")
class Dog {
@Id
private val id: UUID? = null

@Column(name = "DOG_NAME", length = 50, nullable = false, unique = false)
private val name: String? = null

@Column(name = "DOG_AGE")
private val age: Int? = null // getters and setters
// ...
}

// create a named query setting parameters one-by-one, that returns the second page of up to 100 records
val pagedQuery = persistenceService
.query("find_by_name_and_age", Dog::class.java)
.setParameter("name", "Felix")
.setParameter("maxAge", 5)
.setLimit(100)
.setOffset(200)

// execute the query and return the results as a List
val result1 = pagedQuery.execute()

// create a named query setting parameters as Map, that returns the second page of up to 100 records
val paramQuery = persistenceService
.query("find_by_name_and_age", Dog::class.java)
.setParameters(mapOf(Pair("name", "Felix"), Pair("maxAge", 5)))
.setLimit(100)
.setOffset(200)

// execute the query and return the results as a List
val result2 = pagedQuery.execute()
  • Java:

// For JPA Entity:
@CordaSerializable
@Entity
@Table(name = "DOGS")
@NamedQuery(name = "find_by_name_and_age", query = "SELECT d FROM Dog d WHERE d.name = :name AND d.age <= :maxAge")
class Dog {
@Id
private UUID id;
@Column(name = "DOG_NAME", length = 50, nullable = false, unique = false)
private String name;
@Column(name = "DOG_AGE")
private Integer age;

// getters and setters
...
}

// create a named query setting parameters one-by-one, that returns the second page of up to 100 records
ParameterisedQuery<Dog> pagedQuery = persistenceService
.query("find_by_name_and_age", Dog.class)
.setParameter("name", "Felix")
.setParameter("maxAge", 5)
.setLimit(100)
.setOffset(200);

// execute the query and return the results as a List
List<Dog> result1 = pagedQuery.execute();

// create a named query setting parameters as Map, that returns the second page of up to 100 records
ParameterisedQuery<Dog> paramQuery = persistenceService
.query("find_by_name_and_age", Dog.class)
.setParameters(Map.of("name", "Felix", "maxAge", 5))
.setLimit(100)
.setOffset(200);

// execute the query and return the results as a List
List<Dog> result2 = pagedQuery.execute();

Return

A ParameterisedQuery that returns the list of entities found. Empty list if none were found.

Parameters

queryName

The name of the named query registered in the persistence context.

entityClass

The type of the entities to find.

T

The type of the results.

Throws