Skip to content

CRUD opperation services

G-Art edited this page Jul 13, 2021 · 4 revisions

CRUD

For data manipulation porpuces exist two services (ItemService and SearchService)


ItemService

Responsible for executing creation, saveing and removeing operations.

Creation: There are two ways to create new item instance

  1. Create throught constructor
 ExampleItem item = new ExampleItem();
  1. Create throught ItemService
 @Resource
 private ItemService itemService;

 ExampleItem item = itemService.create(ExampleItem.class);

First case suitable for creating item if you need specific type, second suitable for dynamically creating items if type is not available till runtime.

Save & Update:

Update and saveing is combined in one operation save and saveAll(for collection).

Doesn't matter if it's newly created item or modified pre saved, method save will check and perform correspondent operation implicit.

Example:

 @Resource
 private ItemService itemService;

 ExampleItem item = itemService.create(ExampleItem.class);
 itemService.save(item);

Removeing

Similar as saving approach, for deleting operation available two methods delete and deleteAll.

Example:

 @Resource
 private ItemService itemService;

 ExampleItem item = itemService.create(ExampleItem.class);
 itemService.save(item);
 itemService.delete(item);

!!!Note!!!

For relation attributes that marked as (associated = true) will be applied cascadian removing.


SearchService

SearchService provides two approaches for query execution regular and pageable

The query will be transformed automatically so recommended to use item name instead of table name and attribute name instead of the column name. All inheritance collisions will be solved during the transformation and corresponded item names will be amended to table name and attribute names will be amended to column name automatically.

Example:

String query = "SELECT * FROM " + ExampleItem.ITEM_TYPE + " AS i ";
		var result = getSearchService().<Map>search(query);
		return result.getResult().iterator().next();

Query used named parameters that represent keys of the map if it's required.

Example:

String query = "SELECT * FROM " + ExampleItem.ITEM_TYPE + " AS i " +
				"WHERE i." + ExampleItem.CREATE_DATE + ">= :from";
		var result = getSearchService().<Map>search(query, Map.of("from", new Date()));
		return result.getResult().iterator().next();

If you need some data that is not represented any item just provide specific attributes between (SELECT <attributes> FROM) and you will ge t list of maps with correspondent fields.

Example:

String query = "SELECT count(i.uuid) FROM " + ExampleItem.ITEM_TYPE + " AS i " +
				"WHERE i." + ExampleItem.CREATE_DATE + ">= :from";
		var result = getSearchService().<Map>search(query, Map.of("from", new Date()));
		return result.getResult().iterator().next();

A pageable query has the same rules but has an ability to provide additional count and page parameters.

Example:

return getSearchService().searchPageable(
				"SELECT i.* FROM " + ExampleItem.ITEM_TYPE + " AS i " +
                                "ORDER BY i." + ExampleItem.CREATE_DATE,
				Map.of("uuid", uuid),
				10, 0);
Clone this wiki locally