Announcement Announcement Module
No announcement yet.
Does CrudRepository explicitly need to be extended? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Does CrudRepository explicitly need to be extended?


    1. I'd like to have basic CRUD functionality for a lot of entities. Non of them should have any specific implementation (yet).
    Do I explicitly have to create an interface that extends CrudRespository<Entity, ID> for every entity? Or can I set up some kind of automatically generic wireing for a set of entities?

    2. What if I want to override a specific method of CrudRepository? I then have to write an implementation, but when implementing CrudRepository, I would have to implement ALL crud methods, not only one.

    interface UserRepository extends CrudRepository<User, Long> {
    class UserRepositoryImpl implements UserRepository {
           public User findOne(Long id) {
               //my custom implementation
         //this would give an error as all other crud methods would have to be expliticly implemented!
    I'd like then to autowire UserRepositoryImpl and just use all crud operations on it. Just the findOne should delegate to UserRepositoryImpl, whereas the rest should use the default funcationality of CrudRepository.

    Is that possible?

  • #2
    I do not think you have right approach.
    Repository are not design to be implemented directly.

    I do not think that what you want is possible.
    You cannot override the default implementation of findOne. You can only define a custom find method as described in the documentation.

    Or you can also use @Query annotation to customize the request.

    Here an example that works very well for me. Or you can look at my github:
    * Cell repository
    * @author vicben01
    public interface CellRepository extends CrudRepository<Cell, Long>
    List<Cell> findByVaultID(Long vaultID);

    public class Cell implements Serializable

    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Long vaultID;

    // Getter and setters

    And a small test:

    package com.arm.pdeg.ipc.simple.domain;

    import com.arm.pdeg.ipc.simple.config.DataConfiguration;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autow ired;
    import org.springframework.test.context.ContextConfigurat ion;
    import org.springframework.test.context.junit4.SpringJUni t4ClassRunner;
    import org.springframework.test.context.transaction.Trans actionConfiguration;

    import javax.transaction.Transactional;
    import java.util.List;

    import static junit.framework.Assert.assertEquals;
    import static junit.framework.Assert.assertNotNull;

    @ContextConfiguration(classes = {DataConfiguration.class})
    @TransactionConfiguration(defaultRollback = true)
    public class CellTest

    CellRepository cellRepository;

    public void all()
    Cell cell = new Cell();

    assertEquals(0, cellRepository.count());;

    assertEquals(1, cellRepository.count());

    List<Cell> cs = cellRepository.findByVaultID(1L);
    assertEquals(1, cs.size());


    assertEquals(0, cellRepository.count());

    Have fun..