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

  • Does CrudRepository explicitly need to be extended?

    Hi,

    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.

    Code:
    interface UserRepository extends CrudRepository<User, Long> {
    }
    
    class UserRepositoryImpl implements UserRepository {
           @Override
           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.
    http://docs.spring.io/spring-data/jp....query-methods

    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: https://github.com/jvmvik/spring4/tree/master/crud
    /**
    * Cell repository
    *
    * @author vicben01
    */
    public interface CellRepository extends CrudRepository<Cell, Long>
    {
    List<Cell> findByVaultID(Long vaultID);
    }

    @Entity
    @Table(name="cell")
    public class Cell implements Serializable
    {

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

    @NotNull
    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;

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

    @Autowired
    CellRepository cellRepository;

    @Test
    public void all()
    {
    assertNotNull(1);
    Cell cell = new Cell();
    cell.setVaultID(ts);

    assertEquals(0, cellRepository.count());

    cellRepository.save(cell);

    assertEquals(1, cellRepository.count());

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

    cellRepository.delete(cs);

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

    Have fun..
    Victor

    Comment

    Working...
    X