public abstract class CrudService<T extends CrudIdentifiable> extends Object implements Serializable
An abstract generic base implementation of a CRUD persistence storage access "service" (a "persistence service"), making it easy to derive a best-practices compliant concrete CRUD service implementation.
This service realizes the basic CRUD operations:
create()
+ save()
findById(Long id)
/ findAll()
save()
delete()
In order to create a CRUD service for an entity type, make sure the entity
implements CrudIdentifiable
(or inherits from
CrudEntity
), implement CrudService
for the entity
and register it as a CDI bean in the container (depending on beans.xml
bean-discovery-mode
, explicit registration may not be
necessary).
As an example, a service implementation for a
Customer
entity can extend CrudService
like this:
public class CustomerService extends CrudService<Customer> { @Override @PersistenceContext protected void setEm(EntityManager em) { super.setEm(em); } @Override public Customer create() { return new Customer(); } @Override public Class<Customer> getModelClass() { return Customer.class; } }
setEm(EntityManager)
method, simply call the
super method. The important part is that you inject your
@PersistenceContext
in this method by annotation.You can immediately use this service in a backing bean with full support for CRUD operations on your persistence storage.
Conveniently, Crudlet also comes with an alternative implementation of
CrudService
named CrudServiceMocked
. As its name
suggests, this implementation's "persistence" functionality is based on a
simple HashMap
storing the saved entities. Whilst of no use in a
real-world production environment, this class might come in handy if you want
to try something out without having a proper database / persistence
configuration set up. You may then use a CrudServiceMocked
implementation as e.g. a @SessionScoped
bean, and later change
to a true CrudService
without any interface changes.
Modifier and Type | Field and Description |
---|---|
protected javax.persistence.EntityManager |
em |
Constructor and Description |
---|
CrudService() |
Modifier and Type | Method and Description |
---|---|
long |
countAll()
Counts the number of entities.
|
abstract T |
create()
Invokes the constructor for the entity type.
|
void |
delete(Long id)
Deletes the entity with the
CrudEntity.getId() provided. |
List<T> |
findAll()
Returns a List of all entities.
|
T |
findById(Long id)
Returns the entity with the
CrudEntity.getId() provided. |
abstract Class<T> |
getModelClass()
Returns the entity type.
|
T |
save(T entity)
Saves / Inserts / Updates the entity provided and returns the updated entity (e.g. updated
CrudEntity.getId() field. |
protected void |
setEm(javax.persistence.EntityManager em)
Sets the entity manager.
|
public abstract T create()
protected void setEm(javax.persistence.EntityManager em)
EntityManager
with the associated @PersistenceContext
public T findById(Long id)
CrudEntity.getId()
provided.public long countAll()
public T save(@NotNull T entity)
CrudEntity.getId()
field.
Note: It's important to continue to work with the newly returned, updated entity rather than with the original entity.public void delete(Long id)
CrudEntity.getId()
provided.Copyright © 2016, codebulb.ch. All rights reserved.