保留实体时出错:FROM中的子查询必须具有别名

时间:2018-11-08 21:01:43

标签: java postgresql spring-boot entity

我试图将类别保留到类别表中,但是出现此错误:错误:FROM中的子查询必须具有别名。仅当我使用Postgres时,才会发生此错误,当我在内存数据库中使用H2时,它工作正常。

这是我的代码:

AbstractEntity

tibble(za) %>%
  mutate(za = map(za, ~ data.frame(t(flatten_dbl(list(.)))))) %>%
  unnest(za) %>%
  summarize_all(quantile, probs = .975) %>%
  matrix(ncol = 2)

类别实体

@MappedSuperclass
public class AbstractEntity {

    @Id
    @GeneratedValue
    private Long id;

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || this.getClass() != obj.getClass()) {
            return false;
        }
        return Objects.equals(this.id, AbstractEntity.class.cast(obj).id);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }

  // getters and setter ommitted for brevity
}

类别存储库

@Entity
@Table(name = "app_category")
public class Category extends AbstractEntity {

    @Column(name = "name", nullable = false)
    private String name;

    @ManyToOne
    @JoinColumn(name = "parentid")
    private Category parent;

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "categories")
    private Set<Product> products;

    @OneToMany(mappedBy = "parent")
    private Set<Category> childCategories;

    // getters and setter ommitted for brevity
 }

CategoryService

@Repository
public interface CategoryRepository extends JpaRepository<Category, Long> {

    String GET_RECURSIVELY_ALL_SUBCATEGORIES_SQL = 

                  "WITH RECURSIVE ALL_SUBCATEGORIES(ID, PARENTID) 
                          AS 
                   (select c.id, c.parentid 
                         from app_category c where c.parentid is null 
                   union all select c.id, c.parentid 
                         from ALL_SUBCATEGORIES 
                   inner join app_category c on ALL_SUBCATEGORIES.id = c.parentid) select id, parentid 
                         from ALL_SUBCATEGORIES";

}

CategoryController

@Transactional
@Override
public Category createCategory(String name) {
    Category category = new Category();
    category.setName(name);

    return categoryRepository.save(category);
}

当我尝试创建类别时,出现错误:

  

无法提取ResultSet; SQL [n / a];嵌套的异常是org.hibernate.exception.SQLGrammarException:无法提取ResultSet

stacktrace还显示以下内容:

@RequestMapping(method = RequestMethod.POST)
@ApiOperation(value = "", authorizations = { @Authorization(value="apiKey") })
public ResponseEntity<?> createCategory(@RequestBody @Valid CategoryDto request) {
    final Category category = categoryService.createCategory(request.getName());

    return ResponseEntity.status(HttpStatus.CREATED).body(categoryResourceAssembler.toResource(category));
}

1 个答案:

答案 0 :(得分:0)

即使在兼容模式下,H2也不是Postgres。这是一个很棒的小系统,但是它仍然只实现Postgres所做的事情的一部分。

我认为,这恰好是错误消息所说的-在子查询上缺少别名,这是SQL标准不允许的。 Postgres在遵循该标准方面往往非常严格。

更具体地说,WITH内部的SUB_CATEGORIES上没有别名,这可能是您的问题。

WITH RECURSIVE all_subcategories(id, parentid) AS (
    SELECT c.id, c.parentid 
    FROM app_category c
    WHERE c.parentid is null 
    UNION ALL
    SELECT c.id, c.parentid 
    FROM all_subcategories sc 
         INNER JOIN app_category c ON sc.id = c.parentid
)
SELECT id, parentid
FROM all_subcategories;

我怀疑上面的方法应该起作用(假设我没有添加任何错字)。