Category Repository gets fancy.

This commit is contained in:
Tim Holloway 2021-12-28 15:23:20 -05:00
parent 6ba1eb7fea
commit 38455389f7
3 changed files with 39 additions and 1 deletions

View File

@ -1,10 +1,30 @@
package com.mousetech.gourmetj; package com.mousetech.gourmetj;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import com.mousetech.gourmetj.persistence.model.Category; import com.mousetech.gourmetj.persistence.model.Category;
/**
* JpaRepositort for Categories, which relate ManyToOne to Recipe.
* Service method is @see CategoryService
*
* @author timh
* @since Dec 28, 2021
*/
@Repository @Repository
public interface CategoryRepository extends JpaRepository<Category, Long> { public interface CategoryRepository
extends JpaRepository<Category, Long> {
final static String SQL_FIND_CATEGORIES =
"SELECT DISTINCT category from categories"
+ " where category is not null and category <> ''"
+ " ORDER BY category ASC";
@Query(value = SQL_FIND_CATEGORIES, nativeQuery = true)
public List<String> findDistinctCategoryNative();
} }

View File

@ -24,4 +24,12 @@ public class CategoryService implements Serializable {
public List<Category> findAll() { public List<Category> findAll() {
return categoryRepository.findAll(); return categoryRepository.findAll();
} }
/**
*
* @return All Category names that are not null/blank, sorted.
*/
public List<String> findCategoryNames() {
return categoryRepository.findDistinctCategoryNative();
}
} }

View File

@ -7,6 +7,9 @@ import javax.faces.view.ViewScoped;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mousetech.gourmetj.persistence.model.Category; import com.mousetech.gourmetj.persistence.model.Category;
@Named @Named
@ -15,6 +18,8 @@ public class CategoryView implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final Logger log = LoggerFactory.getLogger(CategoryView.class);
@Inject @Inject
private CategoryService categoryRepository; private CategoryService categoryRepository;
@ -23,6 +28,11 @@ public class CategoryView implements Serializable {
@PostConstruct @PostConstruct
public void init() { public void init() {
categories = categoryRepository.findAll(); categories = categoryRepository.findAll();
List<String> catNames = categoryRepository.findCategoryNames();
for (String name : catNames ) {
log.info("CAT="+name);
}
} }
public List<Category> getCategories() { public List<Category> getCategories() {