Interface | Description |
---|---|
PanacheMongoRepository<Entity> |
Represents a Repository for a specific type of entity
Entity , with an ID type
of ObjectId . |
PanacheMongoRepositoryBase<Entity,Id> |
Represents a Repository for a specific type of entity
Entity , with an ID type
of Id . |
PanacheQuery<Entity> |
Class | Description |
---|---|
PanacheMongoEntity |
Represents an entity with a generated ID field
PanacheMongoEntity.id of type ObjectId . |
PanacheMongoEntityBase |
Represents an entity.
|
PanacheMongoEntity
,
and the repository pattern via PanacheMongoRepository
.
To use the active record pattern, make your entities extend PanacheMongoEntity
,
use public fields for your columns, use the existing operations defined as static methods on your entity class,
and define custom ones as static methods on your entity class:
public class Person extends PanacheMongoEntity {
public String name;
public LocalDate birth;
public PersonStatus status;
public static Person findByName(String name){
return find("name", name).firstResult();
}
public static List<Person> findAlive(){
return list("status", Status.Alive);
}
public static void deleteStefs(){
delete("name", "Stef");
}
}
To use the repository pattern, create a class implementing PanacheMongoRepository
,
use the existing operations from your repository and define new ones on your repository class:
@ApplicationScoped
public class PersonRepository implements PanacheRepository<Person> {
public Person findByName(String name){
return find("name", name).firstResult();
}
public List<Person> findAlive(){
return list("status", Status.Alive);
}
public void deleteStefs(){
delete("name", "Stef");
}
}
You can also use PanacheMongoRepository
if you prefer the repository approach.
You can use the @MongoEntity
annotation to define the name of the MongoDB collection,
otherwise it will be the name of your entity.
The Mongo PojoCodec is used to serialize your entity to Bson Document, you can find more information on it's
documentation page: https://mongodb.github.io/mongo-java-driver/3.10/bson/pojos/
You can use the MongoDB annotations to control the mapping to the database : @BsonId
,
@BsonProperty("fieldName")
, @BsonIgnore
.
Normally, MongoDB queries are of this form: {"field1": "value1", "field2": "value2"}
We support multiple convenience query implementations, this is what we called PanacheQL queries:
@BsonProperty
annotations
You can also write native MongoDB queries, in this case the field names are not replaced even if you use
@BsonProperty
, but you can still use parameterized queries by index or name.
find("{'field':?1}", value)
or find("{'field'::key}", value)
Copyright © 2021 JBoss by Red Hat. All rights reserved.