package <%=packageName%>.repository;

import <%=packageName%>.domain.<%=entityClass%>;
<% if (databaseType == 'cassandra') { %>
import com.datastax.driver.core.*;
import com.datastax.driver.mapping.Mapper;
import com.datastax.driver.mapping.MappingManager;<% } %><% if (databaseType=='sql') { %>
import org.springframework.data.jpa.repository.*;<% if (fieldsContainOwnerManyToMany==true) { %>
import org.springframework.data.repository.query.Param;<% } %>

import java.util.List;<% } %><% if (databaseType=='mongodb') { %>
import org.springframework.data.mongodb.repository.MongoRepository;<% } %><% if (databaseType == 'cassandra') { %>
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;<% } %>

<% if (databaseType=='sql') { %>/**
 * Spring Data JPA repository for the <%= entityClass %> entity.
 */<% } %><% if (databaseType=='mongodb') { %>/**
 * Spring Data MongoDB repository for the <%= entityClass %> entity.
 */<% } %><% if (databaseType=='cassandra') { %>/**
 * Cassandra repository for the <%= entityClass %> entity.
 */<% } %><% if (databaseType=='sql' || databaseType=='mongodb') { %>
public interface <%=entityClass%>Repository extends <% if (databaseType=='sql') { %>JpaRepository<% } %><% if (databaseType=='mongodb') { %>MongoRepository<% } %><<%=entityClass%>,<%= pkType %>> {<% for (relationshipId in relationships) { %><% if (relationships[relationshipId].relationshipType == 'many-to-one' && relationships[relationshipId].otherEntityName == 'user') { %>

    @Query("select <%= entityInstance %> from <%= entityClass %> <%= entityInstance %> where <%= entityInstance %>.<%= relationships[relationshipId].relationshipFieldName %>.login = ?#{principal.username}")
    List<<%= entityClass %>> findBy<%= relationships[relationshipId].relationshipNameCapitalized %>IsCurrentUser();<% } } %>
<% if (fieldsContainOwnerManyToMany==true) { %>
    @Query("select distinct <%= entityInstance %> from <%= entityClass %> <%= entityInstance %><% for (relationshipId in relationships) {
    if (relationships[relationshipId].relationshipType == 'many-to-many' && relationships[relationshipId].ownerSide == true) { %> left join fetch <%=entityInstance%>.<%=relationships[relationshipId].relationshipFieldName%>s<%} }%>")
    List<<%=entityClass%>> findAllWithEagerRelationships();

    @Query("select <%= entityInstance %> from <%= entityClass %> <%= entityInstance %><% for (relationshipId in relationships) {
    if (relationships[relationshipId].relationshipType == 'many-to-many' && relationships[relationshipId].ownerSide == true) { %> left join fetch <%=entityInstance%>.<%=relationships[relationshipId].relationshipFieldName%>s<%} }%> where <%=entityInstance%>.id =:id")
    <%=entityClass%> findOneWithEagerRelationships(@Param("id") Long id);
<% } %>
}<% } %><% if (databaseType == 'cassandra') { %>
@Repository
public class <%= entityClass %>Repository {

    @Inject
    private Session session;

    private Mapper<<%= entityClass %>> mapper;

    private PreparedStatement findAllStmt;

    private PreparedStatement truncateStmt;

    @PostConstruct
    public void init() {
        mapper = new MappingManager(session).mapper(<%= entityClass %>.class);
        findAllStmt = session.prepare("SELECT * FROM <%= entityInstance %>");
        truncateStmt = session.prepare("TRUNCATE <%= entityInstance %>");
    }

    public List<<%= entityClass %>> findAll() {
        List<<%= entityClass %>> <%= entityInstancePlural %> = new ArrayList<>();
        BoundStatement stmt =  findAllStmt.bind();
        session.execute(stmt).all().stream().map(
            row -> {
                <%= entityClass %> <%= entityInstance %> = new <%= entityClass %>();
                <%= entityInstance %>.setId(row.getUUID("id"));<%
                for (fieldId in fields) {
                    var fieldInJavaBeanMethod = fields[fieldId].fieldInJavaBeanMethod;
                    var fieldName = fields[fieldId].fieldName;
                    if (fields[fieldId].fieldType == 'Integer') { %>
                <%= entityInstance %>.set<%= fieldInJavaBeanMethod %>(row.getInt("<%= fieldName %>"));<% } else if (fields[fieldId].fieldType == 'BigDecimal') { %>
                <%= entityInstance %>.set<%= fieldInJavaBeanMethod %>(row.getDecimal("<%= fieldName %>"));<% } else if (fields[fieldId].fieldType == 'Boolean') { %>
                <%= entityInstance %>.set<%= fieldInJavaBeanMethod %>(row.getBool("<%= fieldName %>"));<% } else if (fields[fieldId].fieldType == 'Text') { %>
                <%= entityInstance %>.set<%= fieldInJavaBeanMethod %>(row.getString("<%= fieldName %>"));<% } else { %>
                <%= entityInstance %>.set<%= fieldInJavaBeanMethod %>(row.get<%= fields[fieldId].fieldType %>("<%= fieldName %>"));<% } } %>
                return <%= entityInstance %>;
            }
        ).forEach(<%= entityInstancePlural %>::add);
        return <%= entityInstancePlural %>;
    }

    public <%= entityClass %> findOne(UUID id) {
        return mapper.get(id);
    }

    public <%= entityClass %> save(<%= entityClass %> <%= entityInstance %>) {
        if (<%= entityInstance %>.getId() == null) {
            <%= entityInstance %>.setId(UUID.randomUUID());
        }
        mapper.save(<%= entityInstance %>);
        return <%= entityInstance %>;
    }

    public void delete(UUID id) {
        mapper.delete(id);
    }

    public void deleteAll() {
        BoundStatement stmt =  truncateStmt.bind();
        session.execute(stmt);
    }
}<% } %>

@@ -3869,32 +3869,16 @@
 id%22));%3C%25
-%0A               
  for (fi
@@ -3899,178 +3899,13 @@
 s) %7B
-%0A                    var fieldInJavaBeanMethod = fields%5BfieldId%5D.fieldInJavaBeanMethod;%0A                    var fieldName = fields%5BfieldId%5D.fieldName;%0A                   
+ %25%3E%3C%25
  if 
@@ -3987,32 +3987,48 @@
 tance %25%3E.set%3C%25= 
+fields%5BfieldId%5D.
 fieldInJavaBeanM
@@ -4048,24 +4048,40 @@
 getInt(%22%3C%25= 
+fields%5BfieldId%5D.
 fieldName %25%3E
@@ -4183,32 +4183,48 @@
 tance %25%3E.set%3C%25= 
+fields%5BfieldId%5D.
 fieldInJavaBeanM
@@ -4248,24 +4248,40 @@
 ecimal(%22%3C%25= 
+fields%5BfieldId%5D.
 fieldName %25%3E
@@ -4380,32 +4380,48 @@
 tance %25%3E.set%3C%25= 
+fields%5BfieldId%5D.
 fieldInJavaBeanM
@@ -4442,24 +4442,40 @@
 etBool(%22%3C%25= 
+fields%5BfieldId%5D.
 fieldName %25%3E
@@ -4571,32 +4571,48 @@
 tance %25%3E.set%3C%25= 
+fields%5BfieldId%5D.
 fieldInJavaBeanM
@@ -4635,24 +4635,40 @@
 String(%22%3C%25= 
+fields%5BfieldId%5D.
 fieldName %25%3E
@@ -4727,24 +4727,40 @@
 e %25%3E.set%3C%25= 
+fields%5BfieldId%5D.
 fieldInJavaB
@@ -4813,24 +4813,40 @@
 ype %25%3E(%22%3C%25= 
+fields%5BfieldId%5D.
 fieldName %25%3E

Revert "polish entity repository class"