【问题标题】:Spring repository custom methodsSpring存储库自定义方法
【发布时间】:2018-01-05 02:17:47
【问题描述】:

我正在尝试向 spring 数据存储库添加一些自定义功能。 以此为起点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 ...
}

不幸的是,我收到以下错误:

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

如果我没记错的话,这意味着 spring 正在尝试基于方法“checkin”生成查询,并且它在 MyEntity 中找不到名为“checkin”的字段。这是正确的,没有这样的领域。我如何让它停止这样做?基于上面的链接,我认为它不应该尝试为这种方法生成查询,但它似乎仍然在这样做。我可能遗漏了一些东西,通常是这样,但我看不出它是什么。

【问题讨论】:

    标签: java spring repository spring-data-jpa


    【解决方案1】:

    如您链接到的参考文档部分所述,您需要一个实现自定义方法的MyDaoImpl 类。我想最简单的方法是将 BatchTableLockImpl 重命名为该名称,或者创建一个空的 MyDaoImpl 扩展该类。

    【讨论】:

    • 感谢 Oliver 很抱歉造成混乱和我对文档的误解。
    猜你喜欢
    • 2017-06-18
    • 2015-08-17
    • 2015-05-10
    • 2017-02-09
    • 2019-06-25
    • 2014-02-02
    • 2016-05-14
    • 2016-03-27
    • 2018-02-09
    相关资源
    最近更新 更多