@DeclareRoles(value="editor") public abstract class AbstractCrudService extends Object implements CrudWriteService, CrudReadService
This base class enables you to create crud service beans with minimal effort. All you have to do is override, implement getEntityManager and annotate with for example @Stateless, @Local(Auditing.class, CrudWriteService.class). You can also just define CDI beans instead of EJB, but be aware you won't get declarative security then.
Inject the crud interfaces using @Inject where you need them.
This base class declares Transactional and EDITORROLE to
protect write operations, read operations by default support no transactions.
When overriding don't forget transaction and role annotations are not
inherited. If you don't need security, override and use
PermitAll.
A JpqlBuilder is injected and used for building dynamic queries and
for setting values when using named queries.
CrudReadService.SORTORDER| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_PAGE_SIZE
default page size (max results), used when no maximum is given.
|
static String |
EDITORROLE |
| Constructor and Description |
|---|
AbstractCrudService() |
| Modifier and Type | Method and Description |
|---|---|
int |
batchDelete(Collection<? extends EntityInterface> t,
Integer flushCount)
save multiple entities, NOTE that normally only after commit or flush
entitymanagers will reflect changes caused by saving all entities, this
may cause entitylisteners to fail.
|
<T extends EntityInterface> |
batchSave(Collection<T> entities,
Integer flushCount)
save multiple entities, NOTE that normally only after commit or flush
entitymanagers will reflect changes caused by saving all entities, this
may cause entitylisteners to fail.
|
int |
count(String namedQuery,
List<Param> params)
Call this using the same parameters as in
CrudReadService.find(java.lang.String, java.util.List, java.lang.Integer, java.lang.Integer, java.lang.Class)
to get to know the total number of results |
int |
countDynamic(List<Param> params,
Class type)
Call this using the same parameters as in findDynamic to get to know the total number of results
|
<T> T |
create(T t)
When overriding look into roles to use, these are not inherited.
|
void |
delete(EntityInterface t)
When overriding look into roles to use, these are not inherited.
|
<T> T |
find(Serializable id,
Class<T> type) |
<T> List<T> |
find(String namedQuery,
List<Param> params,
Integer first,
Integer max,
Class<T> type) |
<T> List<T> |
findAll(Class<T> type) |
<T> List<T> |
findDynamic(Integer first,
Integer max,
Map<String,CrudReadService.SORTORDER> sort,
List<Param> params,
Class<T> type) |
<T> List<T> |
findNative(String namedNativeQuery,
List<Param> params,
Integer first,
Integer max,
Class<T> type) |
<T> T |
findOne(String namedQuery,
List<Param> params,
Class<T> type)
return one result or null, multiple results throws an exception
|
int |
getDefaultPageSize()
Return a default page size when no max is given, negative value means no limit
|
<T extends EntityInterface> |
save(T t)
When overriding look into roles to use, these are not inherited.
|
protected void |
setJpqlBuilder(JpqlBuilder jpqlBuilder)
override to inject your own version of JpqlBuilder
|
<T> Stream<T> |
stream(String namedQuery,
List<Param> params,
Integer first,
Integer max,
Class<T> type) |
<T> Stream<T> |
streamAll(Class<T> type) |
<T> Stream<T> |
streamDynamic(Integer first,
Integer max,
Map<String,CrudReadService.SORTORDER> sort,
List<Param> params,
Class<T> type) |
<T> Stream<T> |
streamNative(String namedNativeQuery,
List<Param> params,
Integer first,
Integer max,
Class<T> type) |
<T> T |
update(T t)
When overriding look into roles to use, these are not inherited.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetEntityManagerpublic static final int DEFAULT_PAGE_SIZE
public static final String EDITORROLE
public <T> T find(Serializable id, Class<T> type)
find in interface CrudReadServicepublic <T> List<T> findAll(Class<T> type)
findAll in interface CrudReadServicepublic <T> List<T> findDynamic(Integer first, Integer max, Map<String,CrudReadService.SORTORDER> sort, List<Param> params, Class<T> type)
findDynamic in interface CrudReadServicefirst - defaults to 0max - defaults to CrudReadService.getDefaultPageSize()public int countDynamic(List<Param> params, Class type)
CrudReadServicecountDynamic in interface CrudReadServicepublic <T> List<T> find(String namedQuery, List<Param> params, Integer first, Integer max, Class<T> type)
find in interface CrudReadServicefirst - defaults to 0max - defaults to CrudReadService.getDefaultPageSize()public <T> List<T> findNative(String namedNativeQuery, List<Param> params, Integer first, Integer max, Class<T> type)
findNative in interface CrudReadServicefirst - defaults to 0max - defaults to CrudReadService.getDefaultPageSize()public int count(String namedQuery, List<Param> params)
CrudReadServiceCrudReadService.find(java.lang.String, java.util.List, java.lang.Integer, java.lang.Integer, java.lang.Class)
to get to know the total number of resultscount in interface CrudReadServicepublic int getDefaultPageSize()
CrudReadServicegetDefaultPageSize in interface CrudReadServiceDEFAULT_PAGE_SIZEpublic <T> T findOne(String namedQuery, List<Param> params, Class<T> type)
CrudReadServicefindOne in interface CrudReadService@RolesAllowed(value="editor") public <T> T create(T t)
PermitAll.create in interface CrudWriteServiceT - t - @RolesAllowed(value="editor") public <T extends EntityInterface> T save(T t)
PermitAll.save in interface CrudWriteServiceT - t - @RolesAllowed(value="editor") public <T> T update(T t)
PermitAll.update in interface CrudWriteServiceT - t - @RolesAllowed(value="editor") public void delete(EntityInterface t)
PermitAll.delete in interface CrudWriteServicet - @RolesAllowed(value="editor") public <T extends EntityInterface> Stream<T> batchSave(Collection<T> entities, Integer flushCount)
CrudWriteServiceTransactional.TxType on the bean that you use in your listeners,
use isolation level 0 (not recommended) or use XA when you need to access
resources from your listeners. When flushCount != null flushes and clears
the entitymanager every flushCount entities. NOTE that this method
executes in one (possibly big) sql transaction! You can use
EntityException in for example your listeners to find out in case
of failure which enitity caused it.batchSave in interface CrudWriteServiceflushCount - when not null try to optimize (flush/clear) every so
many entitiesUtil.getBean(Class, Annotation...)@RolesAllowed(value="editor") public int batchDelete(Collection<? extends EntityInterface> t, Integer flushCount)
CrudWriteServiceTransactional.TxType on the bean that you use in your listeners,
use isolation level 0 (not recommended) or use XA when you need to access
resources from your listeners. When flushCount != null flushes and clears
the entitymanager every flushCount entities. NOTE that this method
executes in one (possibly big) sql transaction! You can use
EntityException in for example your listeners to find out in case
of failure which enitity caused it.batchDelete in interface CrudWriteServiceflushCount - when not null try to optimize (flush/clear) every so
many entitiesUtil.getBean(Class, Annotation...)public <T> Stream<T> streamAll(Class<T> type)
streamAll in interface CrudReadServicepublic <T> Stream<T> streamDynamic(Integer first, Integer max, Map<String,CrudReadService.SORTORDER> sort, List<Param> params, Class<T> type)
streamDynamic in interface CrudReadServicefirst - defaults to 0max - defaults to CrudReadService.getDefaultPageSize()public <T> Stream<T> stream(String namedQuery, List<Param> params, Integer first, Integer max, Class<T> type)
stream in interface CrudReadServicefirst - defaults to 0max - defaults to CrudReadService.getDefaultPageSize()public <T> Stream<T> streamNative(String namedNativeQuery, List<Param> params, Integer first, Integer max, Class<T> type)
streamNative in interface CrudReadServicefirst - defaults to 0max - defaults to CrudReadService.getDefaultPageSize()@Inject protected void setJpqlBuilder(JpqlBuilder jpqlBuilder)
jpqlBuilder - Copyright © 2018–2019 Fryske Akademy. All rights reserved.