Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use javax @Transaction and micronaut @ReadOnly on service methods #102

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion generators/common/templates/README.md.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ will generate few files:
./gradlew openApiGenerate
```
<%_ } _%>
Then implements the generated delegate classes with `@Service` classes.
Then implements the generated delegate classes with `@Singleton` classes.

To edit the `api.yml` definition file, you can use a tool such as [Swagger-Editor](). Start a local instance of the swagger-editor using docker by running: `docker-compose -f src/main/docker/swagger-editor.yml up -d`. The editor will then be reachable at [http://localhost:7742](http://localhost:7742).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ for (const idx in relationships) { if (relationships[idx].relationshipType === '
* Get all the <%= entityInstancePlural %> where <%= relationships[idx].relationshipNameCapitalized %> is {@code null}.
* @return the list of entities.
*/<% if (databaseType === 'sql') { %>
@Transactional(readOnly = true) <% } %>
@ReadOnly
@Transactional<% } %>
public List<<%= instanceType %>> findAllWhere<%= relationships[idx].relationshipNameCapitalized %>IsNull() {
log.debug("Request to get all <%= entityInstancePlural %> where <%= relationships[idx].relationshipNameCapitalized %> is null");
return StreamSupport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ import javax.persistence.criteria.JoinType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import io.micronaut.data.model.Page;
import io.micronaut.data.model.Pageable;
import io.micronaut.transaction.annotation.ReadOnly;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import io.github.jhipster.service.QueryService;

Expand All @@ -61,8 +60,9 @@ import <%= packageName %>.service.mapper.<%= entityClass %>Mapper;
* in a way that all the filters must apply.
* It returns a {@link List} of {@link <%= instanceType %>} or a {@link Page} of {@link <%= instanceType %>} which fulfills the criteria.
*/
@Service<% if (databaseType === 'sql') { %>
@Transactional(readOnly = true)<% } %>
@Singleton<% if (databaseType === 'sql') { %>
@ReadOnly
@Transactional<% } %>
public class <%= serviceClassName %> extends QueryService<<%= asEntity(entityClass) %>> {

private final Logger log = LoggerFactory.getLogger(<%= serviceClassName %>.class);
Expand All @@ -73,7 +73,8 @@ public class <%= serviceClassName %> extends QueryService<<%= asEntity(entityCla
* @param criteria The object which holds all the filters, which the entities should match.
* @return the matching entities.
*/
@Transactional(readOnly = true)
@ReadOnly
@Transactional
public List<<%= instanceType %>> findByCriteria(<%= criteria %> criteria) {
log.debug("find by criteria : {}", criteria);
final Specification<<%= asEntity(entityClass) %>> specification = createSpecification(criteria);
Expand All @@ -90,7 +91,8 @@ public class <%= serviceClassName %> extends QueryService<<%= asEntity(entityCla
* @param page The page, which should be returned.
* @return the matching entities.
*/
@Transactional(readOnly = true)
@ReadOnly
@Transactional
public Page<<%= instanceType %>> findByCriteria(<%= criteria %> criteria, Pageable page) {
log.debug("find by criteria : {}, page: {}", criteria, page);
final Specification<<%= asEntity(entityClass) %>> specification = createSpecification(criteria);
Expand All @@ -107,7 +109,8 @@ public class <%= serviceClassName %> extends QueryService<<%= asEntity(entityCla
* @param criteria The object which holds all the filters, which the entities should match.
* @return the number of matching entities.
*/
@Transactional(readOnly = true)
@ReadOnly
@Transactional
public long countByCriteria(<%= criteria %> criteria) {
log.debug("count by criteria : {}", criteria);
final Specification<<%= asEntity(entityClass) %>> specification = createSpecification(criteria);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import <%= packageName %>.domain.<%= asEntity(entityClass) %>;
<%_ } _%>
<%_ if (pagination !== 'no' || fieldsContainOwnerManyToMany === true) { _%>

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import io.micronaut.data.model.Page;
import io.micronaut.data.model.Pageable;
<%_ } _%>
<%_ if (reactiveRepositories) { _%>
import reactor.core.publisher.Flux;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

<%_ if (pagination !== 'no' || fieldsContainOwnerManyToMany === true) { _%>
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import io.micronaut.data.model.Page;
import io.micronaut.data.model.Pageable;
<%_ } _%>
import org.springframework.stereotype.Service;
import javax.inject.Singleton;
<%_ if (databaseType === 'sql') { _%>
import org.springframework.transaction.annotation.Transactional;
import javax.transaction.Transactional;
import io.micronaut.transaction.annotation.ReadOnly;
<%_ } _%>
<%_ if (reactiveRepositories) { _%>
import reactor.core.publisher.Flux;
Expand Down Expand Up @@ -99,7 +100,7 @@ import static org.elasticsearch.index.query.QueryBuilders.*;
/**
* Service Implementation for managing {@link <%= asEntity(entityClass) %>}.
*/
@Service<% if (databaseType === 'sql') { %>
@Singleton<% if (databaseType === 'sql') { %>
@Transactional<% } %>
public class <%= serviceClassName %><% if (service === 'serviceImpl') { %> implements <%= entityClass %>Service<% } %> {

Expand Down Expand Up @@ -130,7 +131,8 @@ public class <%= serviceClassName %><% if (service === 'serviceImpl') { %> imple
@Override
<%_ } _%>
<%_ if (databaseType === 'sql') { _%>
@Transactional(readOnly = true)
@ReadOnly
@Transactional
<%_ } _%>
public <% if (pagination !== 'no') { %>Page<<%= instanceType %><% } else { %>List<<%= instanceType %><% } %>> findAll(<% if (pagination !== 'no') { %>Pageable pageable<% } %>) {
log.debug("Request to get all <%= entityClassPlural %>");
Expand Down Expand Up @@ -177,7 +179,8 @@ public class <%= serviceClassName %><% if (service === 'serviceImpl') { %> imple
@Override
<%_ } _%>
<%_ if (databaseType === 'sql') { _%>
@Transactional(readOnly = true)
@ReadOnly
@Transactional
<%_ } _%>
public Optional<<%= instanceType %>> findOne(<%= primaryKeyType %> id) {
log.debug("Request to get <%= entityClass %> : {}", id);<%- include('../../common/get_template', {asEntity, asDto, viaService, returnDirectly:true}); -%>
Expand Down Expand Up @@ -208,7 +211,8 @@ public class <%= serviceClassName %><% if (service === 'serviceImpl') { %> imple
@Override
<%_ } _%>
<%_ if (databaseType === 'sql') { _%>
@Transactional(readOnly = true)
@ReadOnly
@Transactional
<%_ } _%>
public <% if (pagination !== 'no') { %>Page<<%= instanceType %><% } else { %>List<<%= instanceType %><% } %>> search(String query<% if (pagination !== 'no') { %>, Pageable pageable<% } %>) {
<%_ if (pagination === 'no') { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import io.micronaut.context.annotation.Value;
<%_ if (pagination !== 'no') { _%>
import io.micronaut.data.model.Page;
import io.micronaut.data.model.Pageable;
import io.micronaut.transaction.annotation.ReadOnly;
<%# import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; %>
<%_ if (reactive) { _%>
Expand Down Expand Up @@ -251,7 +252,8 @@ public class <%= entityClass %>Resource {
*/
@Get("/<%= entityApiUrl %><% if (pagination !== 'no') { _%>{?eagerload}<% } %>")
<%_ if (databaseType === 'sql' && isUsingMapsId === true && !viaService) { _%>
@Transactional(readOnly = true)
@ReadOnly
@Transactional
<%_ } _%>
<%- include('../../common/get_all_template', {asEntity, asDto, viaService}); -%>

Expand All @@ -262,7 +264,8 @@ public class <%= entityClass %>Resource {
*/
@Get(value = "/<%= entityApiUrl %>", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
<%_ if (databaseType === 'sql' && isUsingMapsId === true && !viaService) { _%>
@Transactional(readOnly = true)
@ReadOnly
@Transactional
<%_ } _%>
public Flux<<%= instanceType %>> getAll<%= entityClassPlural %>AsStream() {
log.debug("REST request to get all <%= entityClassPlural %> as a stream");
Expand All @@ -282,7 +285,8 @@ public class <%= entityClass %>Resource {
*/
@Get("/<%= entityApiUrl %>/{id}")
<%_ if (databaseType === 'sql' && isUsingMapsId === true && !viaService) { _%>
@Transactional(readOnly = true)
@ReadOnly
@Transactional
<%_ } _%>
public Optional<<%= instanceType %>> get<%= entityClass %>(@PathVariable <%= primaryKeyType %> id) {
log.debug("REST request to get <%= entityClass %> : {}", id);
Expand Down