peekay
peekay

Reputation: 1271

Spring repository custom methods

I am trying to add some custom functionality to a spring data repository. 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:

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'. 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.

Upvotes: 1

Views: 254

Answers (1)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83081

As stated in the reference documentation section you linked to, you need a MyDaoImpl class that implements the custom methods. I guess the easiest way is to either rename BatchTableLockImpl to that or just create an empty MyDaoImpl extending that class.

Upvotes: 2

Related Questions