【问题标题】:Creating composable repositories in micronaut在 micronaut 中创建可组合的存储库
【发布时间】:2020-02-23 02:21:39
【问题描述】:

我正在尝试使用 micronaut 中的可组合存储库来实现一种方法。

我有:

@Repository
public interface EmployeeRepository extends CrudRepository<Employee, Long>, EmployeeRepositoryCustom {
}

这里,EmployeeRepositoryCustom 是一个带有方法 'list()' 的接口:

public interface EmployeeRepositoryCustom {
    List<Employee> list();
}

然后我有实现接口方法的 EmployeeRepositoryCustomImpl 类:

public class EmployeeRepositoryCustomImpl implements EmployeeRepositoryCustom {
    @PersistenceContext
    EntityManager em;

    @Override
    @Transactional
    public List<Employee> list() {

       // implementation code
    }
}

当我使用以下方法调用方法时:

@Inject

EmployeeRepository employeeRepository;

public List<Employee> get(){
     return employeeRepository.list();
}

我收到以下消息:

java.lang.IllegalStateException: Micronaut Data method is missing compilation time query information. Ensure that the Micronaut Data annotation processors are declared in your build and try again with a clean re-build. 


我尝试在 EmployeeRepositoryCustom 和 EmployeeRepositoryCustomImpl 上添加注释 @Repository,但它仍然给出相同的错误消息。有什么办法可以做到吗?

我知道我可以只注入 EmployeeRepositoryCustom 类并访问该方法,但我想使用可组合存储库方法来完成。因为,员工存储库来自另一个模式(不是默认数据源,而是另一个数据源),我希望能够指定如下模式:

@Repository("schema2")

【问题讨论】:

    标签: micronaut micronaut-data


    【解决方案1】:

    您不应该创建接口的实现。如果您想使用自己的代码创建方法,可以将存储库创建为抽象类,并将您想要为您实现的方法保留为抽象,然后您可以创建任何具体方法。

    【讨论】:

    • 是的,但我想使用可组合存储库模式来做到这一点。
    猜你喜欢
    • 2012-06-01
    • 2022-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多