2017-07-28 67 views
0

我想将一些自定义功能添加到春季资料库。 使用这个作为我的出发点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! 

此,如果我没有记错意味着弹簧试图根据方法'checkin'生成一个查询,并且它无法在名称'checkin'中找到MyEntity中的字段。这是正确的,没有这样的领域。我如何让它停止这样做?基于上面的链接,我不认为它应该试图为这个方法生成一个查询,但它似乎正在做它无论如何。我可能会错过一些东西,通常是这样,但我不明白它是什么。

回答

2

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

+0

谢谢奥利弗对于文档的混淆和误解感到抱歉。 – peekay