Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Maven接口方法之间的不明确引用_Java_Maven_Jpa_Repository - Fatal编程技术网

Java Maven接口方法之间的不明确引用

Java Maven接口方法之间的不明确引用,java,maven,jpa,repository,Java,Maven,Jpa,Repository,我有一个JavaSpring项目,使用EclipseLink实现JPA持久化。我想为我的实体和大多数情况下的默认实现使用JpaRepository接口,但我还需要定义一些我自己的方法,有时还需要覆盖默认方法,如save 我的代码在Eclipse中编译时可以工作,但在使用Maven编译时,我总是遇到一个不明确的引用错误 我所做的是(例如,覆盖保存,因为我需要对要保存的实体执行某些操作): public interface ReportRepository扩展了JpaRepository、Repor

我有一个JavaSpring项目,使用EclipseLink实现JPA持久化。我想为我的实体和大多数情况下的默认实现使用JpaRepository接口,但我还需要定义一些我自己的方法,有时还需要覆盖默认方法,如save

我的代码在Eclipse中编译时可以工作,但在使用Maven编译时,我总是遇到一个不明确的引用错误

我所做的是(例如,覆盖保存,因为我需要对要保存的实体执行某些操作):

public interface ReportRepository扩展了JpaRepository、ReportRepositoryCustom{
}
公共接口ReportRepositoryCustom{
公共报告保存(报告);
进口商的公共int GetReportCount(长期进口ID);
...
}
公共类ReportRepositoryCustomImplementation实现ReportRepositoryCustom{
公共报告保存(报告报告){…}
public int getReportCountForImporter(长导入ID){…}
}
公共类报告服务{
@自动连线
私有报告存储库;
}
当我将Eclipse编译为在Tomcat上运行时,它在Eclipse中工作得很好。对象ReportRepository ReportRepository包含来自JPA存储库实现的方法和我的自定义方法,当我调用ReportRepository.save(…)时,将调用自定义save方法。但是,当我安装Maven时,编译器会抱怨引用不明确:

[错误] /C:/Users/Jarno/git/Korjaamotestiraportointi/src/main/java/fi/testcenter/service/ReportService.java:[40,40] 两种方法对保存的引用都不明确 在中保存(fi.testcenter.domain.report.report) fi.testcenter.repository.ReportRepository自定义和方法保存 在org.springframework.data.repository.crudepository匹配中

我发现编写存储库有点复杂。我希望使用JPA存储库的现成实现,而不必编写任何额外的代码。我的代码保持一切整洁。在服务中用作引用的存储库接口对每个实体的命名方式相同,方法的命名方式也相同,任何自定义方法或重写都是通过自定义接口和实现完成的。我不需要在任何地方编写任何不必要的代码。但后来我遇到了与Maven的问题

通过首先在EclipseTomcat服务器上运行Maven,我成功地用Maven编译了我的代码。但是如果我进行Maven清理,然后安装Maven,我会得到一堆错误。显然,我不想在使用Maven编译时求助于任何黑客


那么,是否有一个修复程序允许对Maven执行此操作?或者有没有其他方法来编码我想在这里做的事情?

所以在大量的谷歌搜索等之后,似乎不可能为Maven的编译器定义哪些保存方法是主要的,是JpaRepository中的还是自定义存储库中的。我不知道Eclipse使用的编译器是如何做到这一点的,但显然Maven在这里并没有遵循相同的逻辑。这是一个遗憾,因为这种编写自定义方法并超越某些JpaRepository方法的方式将是最干净、最好的方式。有一个@Primary注释用于确定哪个bean是自动连接的主要对象(如果存在多个候选对象),但对于接口实现方法似乎没有等效的解决方案。我还没有找到任何其他不需要编写额外代码的方法。扩展SimpleParepository类似乎是一个有点丑陋的解决方案,因为我必须确保实现被用作JpaRepository实现

所以我决定直接解决这个问题:

public interface ReportRepository {
    public List<Report> findAll();

    public Report findOne(Long id);

    public void delete(Report report);

    public Report save(Report report) throws OptimisticLockException;

    public Long getReportCountForImporter(Long importerId);

    .... [other custom methods]

}

public interface ReportRepositoryDefaultMethods extends JpaRepository<Report, Long> {

}

public class ReportRepositoryImpl implements ReportRepository {

    @PersistenceContext()
    EntityManager entityManager;

    @Autowired
    ReportRepositoryDefaultMethods reportRepositoryDefaultMethods;

    public List<Report> findAll() {
        return reportRepositoryDefaultMethods.findAll();
    }

    public Report findOne(Long id) {
        return reportRepositoryDefaultMethods.findOne(id);
    }

    public void delete(Report report) {
        reportRepositoryDefaultMethods.delete(report);
    }

    @Transactional
    public Report save(Report report) throws OptimisticLockException {
        [custom implementation using entityManager methods]

    }
    .... [other custom methods]
}
公共接口报告存储库{
公共列表findAll();
公共报告完成(长id);
公共作废删除(报告);
公共报告保存(报告报告)抛出乐观锁异常;
进口商的公共Long GetReportCount(Long importerId);
..[其他自定义方法]
}
公共接口ReportRepositoryDefaultMethods扩展了JpaRepository{
}
公共类ReportRepositoryImpl实现ReportRepository{
@PersistenceContext()
实体管理器实体管理器;
@自动连线
ReportRepositoryDefaultMethods ReportRepositoryDefaultMethods;
公共列表findAll(){
return reportRepositoryDefaultMethods.findAll();
}
公共报告完成(长id){
返回reportRepositoryDefaultMethods.findOne(id);
}
公共作废删除(报告){
reportRepositoryDefaultMethods.delete(报表);
}
@交易的
公共报告保存(报告报告)引发OptimisticLockException{
[使用entityManager方法的自定义实现]
}
..[其他自定义方法]
}
这不是一个简洁的解决方案,因为我必须在接口及其实现中包含我使用的默认方法,只需调用标准的JpaRepository方法。但是它可以工作,并且我的ReportRepository接口的使用是干净的,因为我没有自定义方法(如customSave())的自定义名称,但是实现的细节隐藏在实现类中


如果有人有更好的解决方案,一个代码量最少的解决方案,我很想听听。

,为什么要用相同的签名定义自己的解决方案?@Sagar Rohankar重写默认方法,因为在将实体保存到数据库之前,我需要做某些事情。我不想以不同的方式命名该方法,并且必须记住,对于某些实体,我需要使用save以外的名称调用自定义方法。然后不要在
ReportRepositoryCustom
接口中扩展
ReportRepositoryCustom
。这样做的原因是,当ReportRepository自动连接时,它具有默认的方法JpaRepository实现,如delete和我的自定义方法。两者都是需要的。但是Mavens编译器被两个具有相同签名的方法搞混了,我不知道
public interface ReportRepository {
    public List<Report> findAll();

    public Report findOne(Long id);

    public void delete(Report report);

    public Report save(Report report) throws OptimisticLockException;

    public Long getReportCountForImporter(Long importerId);

    .... [other custom methods]

}

public interface ReportRepositoryDefaultMethods extends JpaRepository<Report, Long> {

}

public class ReportRepositoryImpl implements ReportRepository {

    @PersistenceContext()
    EntityManager entityManager;

    @Autowired
    ReportRepositoryDefaultMethods reportRepositoryDefaultMethods;

    public List<Report> findAll() {
        return reportRepositoryDefaultMethods.findAll();
    }

    public Report findOne(Long id) {
        return reportRepositoryDefaultMethods.findOne(id);
    }

    public void delete(Report report) {
        reportRepositoryDefaultMethods.delete(report);
    }

    @Transactional
    public Report save(Report report) throws OptimisticLockException {
        [custom implementation using entityManager methods]

    }
    .... [other custom methods]
}