简体   繁体   English

Spring存储库自定义方法

[英]Spring repository custom methods

I am trying to add some custom functionality to a spring data repository. 我正在尝试向Spring数据存储库添加一些自定义功能。 Using this as my starting point http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.single-repository-behaviour I have created the following code: 以此为起点http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.single-repository-behaviour,我创建了以下代码:

public interface TableLock<T> {
    void checkout(T entity);
    void checkin(T entity, boolean cmpltBatch);
}


public interface BatchTableLock extends TableLock<MyEntity> {

}

public class BatchTableLockImpl implements BatchTableLock {
    private static final Logger logger = LoggerFactory.getLogger(BatchTableLockImpl.class);
    @PersistenceContext(unitName = "mysql")
    private EntityManager em;

    @Override
    public void checkout(MyEntity batch) {
    Long id = batch.getMyEntityId();
    try {
        MyEntity p = em.find(MyEntity.class, id, LockModeType.PESSIMISTIC_WRITE);
        if (p == null) {
            logger.error("checkout : MyEntity id {} must be valid", id);
            throw new PessimisticLockException();
        }
        if (myCondition is true) {
            return;
        }
    } catch (LockTimeoutException | PessimisticLockException e) {
        logger.error("checkout : Unable to get write lock on MyEntity id {}", id, e);
    }
    throw new PessimisticLockException();
}

@Override
public void checkin(MyEntity batch, boolean cmplt) {
    Long id = batch.getMyEntityId();
    try {
        MyEntity p = em.find(MyEntity.class, id, LockModeType.PESSIMISTIC_WRITE);
        if (p == null) {
            logger.error("complete : MyEntity id {} must be valid", id);
            return;
        }
        if (this is true) {
            if (cmplt) {
                yep;
            } else {
                nope;
            }
        } else if (cmplt) {
            logger.error("complete: Unable to complete MyEntity {} with status.", id);
        }
    } catch (LockTimeoutException | PessimisticLockException e) {
        logger.error("complete : Unable to get write lock on MyEntity id {}", id, e);
    }
}

}

@Repository
public interface MyDao extends CrudRepository<MyEntity, BigInteger>, BatchTableLock  {
    ... My queries ...
}

unfortunately I am getting the following error: 不幸的是,我收到以下错误:

org.springframework.data.mapping.PropertyReferenceException: No property checkin found for type MyEntity!

This if I'm not mistaken means that spring is trying to generate a query based on the method 'checkin' and it can't find a field in MyEntity with the name 'checkin'. 如果我没记错的话,这意味着spring试图基于方法'checkin'生成查询,并且在MyEntity中找不到名称为'checkin'的字段。 which is correct there is no such field. 没错,这是正确的。 how do I make it stop doing this? 我如何使其停止这样做? based on the link above I don't think it should be trying to generate a query for this method, but it seems to be doing it anyway. 基于上面的链接,我认为它不应该尝试为此方法生成查询,但是无论如何它似乎都在这样做。 I may be missing something, that is usually the case, but I don't see what it is. 我可能会丢失某些东西,通常是这种情况,但我不知道它是什么。

As stated in the reference documentation section you linked to, you need a MyDaoImpl class that implements the custom methods. 如您链接到的参考文档部分所述,您需要一个MyDaoImpl类来实现自定义方法。 I guess the easiest way is to either rename BatchTableLockImpl to that or just create an empty MyDaoImpl extending that class. 我猜最简单的方法是将BatchTableLockImpl重命名BatchTableLockImpl ,或者只是创建一个扩展该类的空MyDaoImpl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM