Class AbstractCrudService
- All Implemented Interfaces:
CrudReadService,CrudWriteService
- Direct Known Subclasses:
AbstractCrudServiceEnvers
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.
- Author:
- eduard
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.fryske_akademy.services.CrudReadService
CrudReadService.SORTORDER -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intdefault page size (max results), used when no maximum is given.static final String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintbatchDelete(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>
Stream<T>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.intCall this using the same parameters as inCrudReadService.find(java.lang.String, java.util.List, java.lang.Integer, java.lang.Integer, java.lang.Class)to get to know the total number of resultsintcountDynamic(List<Param> params, Class type) Call this using the same parameters as in findDynamic to get to know the total number of results<T> Tcreate(T t) When overriding look into roles to use, these are not inherited.voidWhen overriding look into roles to use, these are not inherited.<T> Tfind(Serializable id, Class<T> type) <T> List<T><T> List<T><T> List<T>findDynamic(Integer first, Integer max, Map<String, CrudReadService.SORTORDER> sort, List<Param> params, Class<T> type) <T> TfindExactlyOne(String namedQuery, List<Param> params, Class<T> type) return one result, no or multiple results throws an exception<T> List<T>possibly unsafefindNative(String sql) possibly unsafe<T> List<T>findNative(String sql, Class<T> clazz) possibly unsafe<T> List<T><T> Treturn one result or null, multiple results throws an exceptionintReturn a default page size when no max is given, negative value means no limitprotected JpqlBuilder<T> Trefresh(T t) refresh an entity from the database<T extends EntityInterface>
Tsave(T t) When overriding look into roles to use, these are not inherited.protected voidsetJpqlBuilder(JpqlBuilder jpqlBuilder) override to inject your own version of JpqlBuilder<T> Stream<T><T> Stream<T><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> Tupdate(T t) When overriding look into roles to use, these are not inherited.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.fryske_akademy.services.CrudWriteService
getEntityManager
-
Field Details
-
DEFAULT_PAGE_SIZE
public static final int DEFAULT_PAGE_SIZEdefault page size (max results), used when no maximum is given.- See Also:
-
EDITORROLE
- See Also:
-
-
Constructor Details
-
AbstractCrudService
public AbstractCrudService()
-
-
Method Details
-
find
- Specified by:
findin interfaceCrudReadService
-
findAll
- Specified by:
findAllin interfaceCrudReadService
-
findDynamic
public <T> List<T> findDynamic(Integer first, Integer max, Map<String, CrudReadService.SORTORDER> sort, List<Param> params, Class<T> type) - Specified by:
findDynamicin interfaceCrudReadService- Parameters:
first- defaults to 0max- defaults toCrudReadService.getDefaultPageSize()- Returns:
-
countDynamic
Description copied from interface:CrudReadServiceCall this using the same parameters as in findDynamic to get to know the total number of results- Specified by:
countDynamicin interfaceCrudReadService- Returns:
-
find
public <T> List<T> find(String namedQuery, List<Param> params, Integer first, Integer max, Class<T> type) - Specified by:
findin interfaceCrudReadServicefirst- defaults to 0max- defaults toCrudReadService.getDefaultPageSize()- Returns:
-
findNative
public <T> List<T> findNative(String namedNativeQuery, List<Param> params, Integer first, Integer max, Class<T> type) - Specified by:
findNativein interfaceCrudReadServicefirst- defaults to 0max- defaults toCrudReadService.getDefaultPageSize()- Returns:
-
count
Description copied from interface:CrudReadServiceCall this using the same parameters as inCrudReadService.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- Specified by:
countin interfaceCrudReadService- Returns:
-
getDefaultPageSize
public int getDefaultPageSize()Description copied from interface:CrudReadServiceReturn a default page size when no max is given, negative value means no limit- Specified by:
getDefaultPageSizein interfaceCrudReadService- Returns:
DEFAULT_PAGE_SIZE
-
findOne
Description copied from interface:CrudReadServicereturn one result or null, multiple results throws an exception- Specified by:
findOnein interfaceCrudReadService- Returns:
-
findExactlyOne
public <T> T findExactlyOne(String namedQuery, List<Param> params, Class<T> type) throws PreventEJBLoggingException Description copied from interface:CrudReadServicereturn one result, no or multiple results throws an exception- Specified by:
findExactlyOnein interfaceCrudReadService- Returns:
- Throws:
PreventEJBLoggingException
-
findJpql
possibly unsafe- Specified by:
findJpqlin interfaceCrudReadService- Type Parameters:
T-- Parameters:
jpql-clazz-- Returns:
-
findNative
possibly unsafe- Specified by:
findNativein interfaceCrudReadService- Type Parameters:
T-- Parameters:
sql-clazz-- Returns:
-
findNative
possibly unsafe- Specified by:
findNativein interfaceCrudReadService- Parameters:
sql-- Returns:
-
create
@RolesAllowed("editor") public <T> T create(T t) When overriding look into roles to use, these are not inherited. If you don't need security, override and usePermitAll.- Specified by:
createin interfaceCrudWriteService- Type Parameters:
T-- Parameters:
t-- Returns:
-
save
When overriding look into roles to use, these are not inherited. If you don't need security, override and usePermitAll.- Specified by:
savein interfaceCrudWriteService- Type Parameters:
T-- Parameters:
t-- Returns:
-
update
@RolesAllowed("editor") public <T> T update(T t) When overriding look into roles to use, these are not inherited. If you don't need security, override and usePermitAll.- Specified by:
updatein interfaceCrudWriteService- Type Parameters:
T-- Parameters:
t-- Returns:
-
delete
When overriding look into roles to use, these are not inherited. If you don't need security, override and usePermitAll.- Specified by:
deletein interfaceCrudWriteService- Parameters:
t-
-
batchSave
@RolesAllowed("editor") public <T extends EntityInterface> Stream<T> batchSave(Collection<T> entities, Integer flushCount) Description copied from interface:CrudWriteServicesave 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. You may need to perform checks on the collection before calling this method. Also note that entitylisteners will execute within the batch transaction, you may need to specifyTransactional.TxTypeon 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 useEntityExceptionin for example your listeners to find out in case of failure which enitity caused it.- Specified by:
batchSavein interfaceCrudWriteServiceflushCount- when not null try to optimize (flush/clear) every so many entities- Returns:
- the saved entities
-
batchDelete
@RolesAllowed("editor") public int batchDelete(Collection<? extends EntityInterface> t, Integer flushCount) Description copied from interface:CrudWriteServicesave 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. You may need to perform checks on the collection before calling this method. Also note that entitylisteners will execute within the batch transaction, you may need to specifyTransactional.TxTypeon 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 useEntityExceptionin for example your listeners to find out in case of failure which enitity caused it.- Specified by:
batchDeletein interfaceCrudWriteServiceflushCount- when not null try to optimize (flush/clear) every so many entities- Returns:
- number of entities deleted
-
streamAll
- Specified by:
streamAllin interfaceCrudReadService
-
streamDynamic
public <T> Stream<T> streamDynamic(Integer first, Integer max, Map<String, CrudReadService.SORTORDER> sort, List<Param> params, Class<T> type) - Specified by:
streamDynamicin interfaceCrudReadService- Parameters:
first- defaults to 0max- defaults toCrudReadService.getDefaultPageSize()- Returns:
-
stream
public <T> Stream<T> stream(String namedQuery, List<Param> params, Integer first, Integer max, Class<T> type) - Specified by:
streamin interfaceCrudReadServicefirst- defaults to 0max- defaults toCrudReadService.getDefaultPageSize()- Returns:
-
streamNative
public <T> Stream<T> streamNative(String namedNativeQuery, List<Param> params, Integer first, Integer max, Class<T> type) - Specified by:
streamNativein interfaceCrudReadServicefirst- defaults to 0max- defaults toCrudReadService.getDefaultPageSize()- Returns:
-
refresh
@RolesAllowed("editor") public <T> T refresh(T t) Description copied from interface:CrudWriteServicerefresh an entity from the database- Specified by:
refreshin interfaceCrudWriteService- Returns:
-
setJpqlBuilder
override to inject your own version of JpqlBuilder- Parameters:
jpqlBuilder-
-
getJpqlBuilder
-