如何使用Micronaut Data JDBC在存储库中创建简单计数查询?

时间:2019-10-27 10:24:53

标签: micronaut micronaut-data

我正在尝试在Micronaut Data JDBC存储库中创建一个计数本机查询。我正在使用:

@JdbcRepository(dialect = Dialect.POSTGRES)
public abstract class BookRepository implements GenericRepository<Book, Long> {
    @Transactional(Transactional.TxType.MANDATORY)
    @Query(value = "select count(*) FROM book WHERE registration_date > :date", nativeQuery = true)
    public abstract long countNow(@NotNull Timestamp date);
}

然后出现以下编译错误:

error: Unable to implement Repository method: BookRepository.countNow(Timestamp date). Query results in a type [my.app.db.Book] whilst method returns an incompatible type: long

我该如何解决?

3 个答案:

答案 0 :(得分:0)

我怀疑本机查询必须返回该存储库的实体。您的情况是Book

但是您是否尝试过使用查询方法而不是自己编写查询。

public abstract long countByRegistrationDateGreatherThan(Timestamp t);

有关更多说明,请参见documentation of Micronaut Data.

答案 1 :(得分:0)

这是 JdbcOperations 的另一个选项:

import React, { useState } from 'react';
import './style.css';

export default function App() {
  const [inputs, setInputs] = useState(Array(10).fill(''));
  const inputChangedHandler = (e, index) => {
    const inputsUpdated = inputs.map((input, i) => {
      if (i === index) {
        return e.target.value;
      } else {
        return input;
      }
    });
    setInputs(inputsUpdated);
  };
  return (
    <div>
      {inputs.map((input, i) => (
        <input onChange={e => inputChangedHandler(e, i)} value={input} />
      ))}
    </div>
  );
}

答案 2 :(得分:0)

这是一个带有 CriteriaBuilder 的示例:

val criteriaBuilder = entityManager.criteriaBuilder
val criteriaQuery = criteriaBuilder.createQuery(Long::class.java)
val root = criteriaQuery.from(EntityClass::class.java)
criteriaQuery.select(criteriaBuilder.count(root.get<Int>("id")))