Category Repository gets fancy.

version2
Tim Holloway 2 years ago
parent 6ba1eb7fea
commit 38455389f7
  1. 22
      src/main/java/com/mousetech/gourmetj/CategoryRepository.java
  2. 8
      src/main/java/com/mousetech/gourmetj/CategoryService.java
  3. 10
      src/main/java/com/mousetech/gourmetj/CategoryView.java

@ -1,10 +1,30 @@
package com.mousetech.gourmetj;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
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
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();
}

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

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

Loading…
Cancel
Save