Class AbstractCrudService
- java.lang.Object
-
- org.fryske_akademy.services.AbstractCrudService
-
- All Implemented Interfaces:
CrudReadService,CrudWriteService
- Direct Known Subclasses:
AbstractCrudServiceEnvers
@DeclareRoles("editor") public abstract class AbstractCrudService extends Object implements CrudWriteService, CrudReadServiceThis 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
TransactionalandEDITORROLEto 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 usePermitAll.A
JpqlBuilderis 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
Fields Modifier and Type Field Description static intDEFAULT_PAGE_SIZEdefault page size (max results), used when no maximum is given.static StringEDITORROLE
-
Constructor Summary
Constructors Constructor Description AbstractCrudService()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intbatchDelete(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.intcount(String namedQuery, List<Param> params)Call 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.voiddelete(EntityInterface t)When overriding look into roles to use, these are not inherited.<T> Tfind(Serializable id, Class<T> type)<T> List<T>find(String jpql, Class<T> clazz)query using your own jpql<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> TfindExactlyOne(String namedQuery, List<Param> params, Class<T> type)return one result, no or multiple results throws an exception<T> List<T>findNative(String sql, Class<T> clazz)query using your own sql<T> List<T>findNative(String namedNativeQuery, List<Param> params, Integer first, Integer max, Class<T> type)<T> TfindOne(String namedQuery, List<Param> params, Class<T> type)return one result or null, multiple results throws an exceptionintgetDefaultPageSize()Return a default page size when no max is given, negative value means no limitprotected JpqlBuildergetJpqlBuilder()<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>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> 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, wait
-
Methods inherited from interface org.fryske_akademy.services.CrudWriteService
getEntityManager
-
-
-
-
Field Detail
-
DEFAULT_PAGE_SIZE
public static final int DEFAULT_PAGE_SIZE
default page size (max results), used when no maximum is given.- See Also:
- Constant Field Values
-
EDITORROLE
public static final String EDITORROLE
- See Also:
- Constant Field Values
-
-
Method Detail
-
find
public <T> T find(Serializable id, Class<T> type)
- Specified by:
findin interfaceCrudReadService
-
findAll
public <T> List<T> findAll(Class<T> type)
- 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
public int countDynamic(List<Param> params, Class type)
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
public int count(String namedQuery, List<Param> params)
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
public <T> T findOne(String namedQuery, List<Param> params, Class<T> type)
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
-
find
public <T> List<T> find(String jpql, Class<T> clazz)
Description copied from interface:CrudReadServicequery using your own jpql- Specified by:
findin interfaceCrudReadService- Returns:
-
findNative
public <T> List<T> findNative(String sql, Class<T> clazz)
Description copied from interface:CrudReadServicequery using your own sql- Specified by:
findNativein interfaceCrudReadService- 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
@RolesAllowed("editor") public <T extends EntityInterface> T save(T t)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
@RolesAllowed("editor") public void delete(EntityInterface t)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
- See Also:
Util.getBean(Class, Annotation...)
-
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
- See Also:
Util.getBean(Class, Annotation...)
-
streamAll
public <T> Stream<T> streamAll(Class<T> type)
- 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
@Inject protected void setJpqlBuilder(JpqlBuilder jpqlBuilder)
override to inject your own version of JpqlBuilder- Parameters:
jpqlBuilder-
-
getJpqlBuilder
protected JpqlBuilder getJpqlBuilder()
-
-