Customization (Block Comments)
Overview
When adding custom methods to generated code such as Repositories, use the block comment approach. This approach ensures that custom code is preserved even when ElastiCORE regenerates the code.
Block Comment Syntax
Custom code must be written between the // !---- and // ----! blocks:
// !-----------------------------------------------------------
@Query("SELECT e FROM Entity e WHERE e.name = :name")
List<Entity> findByName(@Param("name") String name);
// ------------------------------------------------------------!
Rules
Important
- Must start with
// !----and end with// ----! - This block can only be used once within a source file
- After defining one block, multiple developers can contribute code within it
- Custom code written outside the block may be lost during regeneration
Examples
Adding Custom Queries to a Repository
@Repository
public interface ArticleRepository extends JpaRepository<Article, String> {
// Auto-generated methods by ElastiCORE
// ...
// !-----------------------------------------------------------
@Query("SELECT a FROM Article a WHERE a.board.bid = :boardId ORDER BY a.createDate DESC")
List<Article> findRecentByBoard(@Param("boardId") Long boardId);
@Query("SELECT COUNT(a) FROM Article a WHERE a.board.bid = :boardId")
long countByBoard(@Param("boardId") Long boardId);
// ------------------------------------------------------------!
}
Adding Business Logic to a Service
@Service
public class ArticleService {
// Auto-generated methods by ElastiCORE
// ...
// !-----------------------------------------------------------
public List<ArticleDTO> getRecentArticles(Long boardId, int limit) {
// Custom business logic
return articleRepository.findRecentByBoard(boardId)
.stream()
.limit(limit)
.map(this::toDTO)
.collect(Collectors.toList());
}
// ------------------------------------------------------------!
}