gpt4 book ai didi

java - Spring存储库自定义方法

转载 作者:行者123 更新时间:2023-11-30 02:27:02 27 4
gpt4 key购买 nike

我正在尝试向 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”的字段。这是正确的,没有这样的字段。我该如何让它停止这样做?根据上面的链接,我认为它不应该尝试生成此方法的查询,但它似乎无论如何都会这样做。我可能会遗漏一些东西,通常都是这种情况,但我不知道它是什么。

最佳答案

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

关于java - Spring存储库自定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45383188/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com