【发布时间】: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