我正在寻找Spring Data JPA。考虑下面的例子,我将得到所有的crud和查找器功能默认工作,如果我想自定义一个查找器,那么也可以在界面本身轻松完成。

@Transactional(readOnly = true)
public interface AccountRepository extends JpaRepository<Account, Long> {

  @Query("<JPQ statement here>")
  List<Account> findByCustomer(Customer customer);
}

我想知道如何添加一个完整的自定义方法及其实现以上AccountRepository?因为它是一个接口,我不能在那里实现方法。


当前回答

你需要为你的自定义方法创建一个单独的接口:

public interface AccountRepository 
    extends JpaRepository<Account, Long>, AccountRepositoryCustom { ... }

public interface AccountRepositoryCustom {
    public void customMethod();
}

并为该接口提供一个实现类:

public class AccountRepositoryImpl implements AccountRepositoryCustom {

    @Autowired
    @Lazy
    AccountRepository accountRepository;  /* Optional - if you need it */

    public void customMethod() { ... }
}

参见:

4.6 Spring数据存储库的自定义实现 请注意,不同版本之间的命名方案发生了变化。详情见https://stackoverflow.com/a/52624752/66686。

其他回答

考虑到您的代码片段,请注意,您只能将本机对象传递给findBy###方法,假设您想要加载属于某些客户的帐户列表,一个解决方案是这样做,

@Query("Select a from Account a where a."#nameoffield"=?1")
List<Account> findByCustomer(String "#nameoffield");

确保要查询的表的名称与Entity类相同。 关于进一步的实现,请看看这个

公认的答案是可行的,但有三个问题:

在将自定义实现命名为AccountRepositoryImpl时,它使用了一个未记录的Spring Data特性。文档明确指出它必须被称为AccountRepositoryCustomImpl,即自定义接口名加上Impl 不能使用构造函数注入,只能使用@Autowired,这被认为是不好的做法 在自定义实现中有一个循环依赖(这就是为什么不能使用构造函数注入)。

我找到了一个让它变得完美的方法,尽管不是没有使用另一个未记录的Spring Data特性:

public interface AccountRepository extends AccountRepositoryBasic,
                                           AccountRepositoryCustom 
{ 
}

public interface AccountRepositoryBasic extends JpaRepository<Account, Long>
{
    // standard Spring Data methods, like findByLogin
}

public interface AccountRepositoryCustom 
{
    public void customMethod();
}

public class AccountRepositoryCustomImpl implements AccountRepositoryCustom 
{
    private final AccountRepositoryBasic accountRepositoryBasic;

    // constructor-based injection
    public AccountRepositoryCustomImpl(
        AccountRepositoryBasic accountRepositoryBasic)
    {
        this.accountRepositoryBasic = accountRepositoryBasic;
    }

    public void customMethod() 
    {
        // we can call all basic Spring Data methods using
        // accountRepositoryBasic
    }
}

这里还有一个问题需要考虑。有些人希望将自定义方法添加到存储库中将自动将其作为'/search'链接下的REST服务公开。不幸的是,事实并非如此。Spring目前不支持这一点。

这是“设计”特性,spring data rest显式检查方法是否为自定义方法,并且不会将其作为rest搜索链接公开:

private boolean isQueryMethodCandidate(Method method) {    
  return isQueryAnnotationPresentOn(method) || !isCustomMethod(method) && !isBaseClassMethod(method);
}

这是Oliver Gierke的一段话:

这是有意为之。自定义存储库方法不像查询方法那样 它们可以有效地实现任何行为。因此,它目前是 我们不可能决定HTTP方法来公开方法 下。POST是最安全的选择但这与 泛型查询方法(接收GET)。

欲了解更多详细信息,请参阅此问题:https://jira.spring.io/browse/DATAREST-206

如果你想做更复杂的操作,你可能需要访问Spring Data的内部,在这种情况下,下面的工作(作为DATAJPA-422的临时解决方案):

public class AccountRepositoryImpl implements AccountRepositoryCustom {

    @PersistenceContext
    private EntityManager entityManager;

    private JpaEntityInformation<Account, ?> entityInformation;

    @PostConstruct
    public void postConstruct() {
        this.entityInformation = JpaEntityInformationSupport.getMetadata(Account.class, entityManager);
    }
    
    @Override
    @Transactional
    public Account saveWithReferenceToOrganisation(Account entity, long organisationId) {
        entity.setOrganisation(entityManager.getReference(Organisation.class, organisationId));
        return save(entity);
    }

    private Account save(Account entity) {
        // save in same way as SimpleJpaRepository
        if (entityInformation.isNew(entity)) {
            entityManager.persist(entity);
            return entity;
        } else {
            return entityManager.merge(entity);
        }
    }

}

除了axtavt的答案,不要忘记你可以在你的自定义实现中注入实体管理器,如果你需要它来构建你的查询:

public class AccountRepositoryImpl implements AccountRepositoryCustom {

    @PersistenceContext
    private EntityManager em;

    public void customMethod() { 
        ...
        em.createQuery(yourCriteria);
        ...
    }
}