daydreamer
daydreamer

Reputation: 92149

Spring data and Java Generics: How to combine multiple repository interfaces into one?

What I have now
I have 2 repositores (infact many, but for this example lets say 2) and they look like

@Repository
public interface AgeRepository extends JpaSpecificationExecutor<Age>,
        JpaRepository<Age, Long> {
} 

and

@Repository
public interface GenderRepository extends JpaSpecificationExecutor<Gender>,
        JpaRepository<Gender, Long> {
}

All the other repositories look same. The only difference is class name Age, Gender, Network etc

and they have similar implementations. The corresponding implementations for above two are

@Component
@Transactional
public class AgeRepositoryService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AgeRepositoryService.class);
    private AgeRepository ageRepository;

    @SuppressWarnings("UnusedDeclaration")
    public AgeRepositoryService() {
    }

    @Autowired
    public AgeRepositoryService(@Nonnull final AgeRepository ageRepository) {
        this.ageRepository = ageRepository;
    }

    @Nonnull
    public Age save(@Nonnull final Age age) {
        LOGGER.debug("adding age {}", age);
        return ageRepository.saveAndFlush(age);
    }

    @Nonnull
    public List<Age> getAges() {
        return ageRepository.findAll();
    }
}

and

@Component
@Transactional
public class GenderRepositoryService {
    private static final Logger LOGGER = LoggerFactory.getLogger(GenderRepositoryService.class);
    private GenderRepository genderRepository;

    @SuppressWarnings("UnusedDeclaration")
    public GenderRepositoryService() {
    }

    @Autowired
    public GenderRepositoryService(@Nonnull final GenderRepository genderRepository) {
        this.genderRepository = genderRepository;
    }

    @Nonnull
    public Gender save(@Nonnull final Gender gender) {
        LOGGER.debug("adding gender {}", gender);
        return genderRepository.saveAndFlush(gender);
    }

    @Nonnull
    public List<Gender> getGenders() {
        return genderRepository.findAll();
    }
}

calling exactly same methods on repository interface.

What I did
I created a generic repository interface as

@Repository
public interface GenericRepository<T> extends JpaSpecificationExecutor<T>,
        JpaRepository<T, Long> {
}

and in one of the implementation class I tried as

@Component
@Transactional
public class AgeRepositoryService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AgeRepositoryService.class);
    private GenericRepository<Age> ageRepository;

    @SuppressWarnings("UnusedDeclaration")
    public AgeRepositoryService() {
    }

    @Autowired
    public AgeRepositoryService(@Nonnull final GenericRepository<Age> AgeRepository) {
        this.ageRepository = ageRepository;
    }

    @Nonnull
    public Age save(@Nonnull final Age age) {
        LOGGER.debug("adding age {}", age);
        return ageRepository.saveAndFlush(age);
    }

    @Nonnull
    public List<Age> getAges() {
        return ageRepository.findAll();
    }
}

When I run my Tests, maven fails while loading the application context. I error I see is

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ageRepositoryService' defined in file [/Users/harith/IdeaProjects/comma/persistence/target/classes/com/yahoo/comma/persistence/impl/AgeRepositoryService.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.yahoo.comma.persistence.repository.GenericRepository]: : Error creating bean with name 'genericRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'genericRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object

What am I doing wrong?

What I am looking for?
Generify the Repositories into one so that I do not have to write multiple generic classes just because they differ in class Name.

How can I resolve such issues?

UPDATE
My test looks like

public class AgeRepositoryServiceTest extends AbstractUnitTestHelper {

    @Autowired
    AgeRepositoryService ageRepositoryService;

    @Test
    public void testGetAges() {
        Age age_13_17;
        {
            age_13_17 = new Age(1, "13-17", true, DateTime.now(), "pryme_user", DateTime.now(), "pryme_user");
            age_13_17 = ageRepositoryService.save(age_13_17);
            assertNotNull("Age Exists in Database", age_13_17.getId());
        }

        Age age_65_plus;
        {
            age_65_plus = new Age(1, "65+", true, DateTime.now(), "pryme_user", DateTime.now(), "pryme_user");
            age_65_plus = ageRepositoryService.save(age_65_plus);
            assertNotNull("Age Exists in Database", age_65_plus.getId());
        }

        {
            final List<Age> ages = ageRepositoryService.getAges();
            assertFalse(ages.isEmpty());
            assertEquals(2, ages.size());

            assertEquals(age_13_17.getAgeId(), ages.get(0).getAgeId());
            assertEquals(age_13_17.getName(), ages.get(0).getName());
            assertEquals("Age must be active", age_13_17.isActive(), ages.get(0).isActive());
            assertEquals(age_13_17.getCreatedAt(), ages.get(0).getCreatedAt());
            assertNotNull(ages.get(0).getCreatedBy());
            assertNotNull(ages.get(0).getUpdatedAt());
            assertNotNull(ages.get(0).getUpdatedBy());

            assertEquals(age_65_plus.getAgeId(), ages.get(1).getAgeId());
            assertEquals(age_65_plus.getName(), ages.get(1).getName());
            assertEquals("Age must be active", age_13_17.isActive(), ages.get(1).isActive());
            assertEquals(age_65_plus.getCreatedAt(), ages.get(1).getCreatedAt());
            assertNotNull(ages.get(1).getCreatedBy());
            assertNotNull(ages.get(1).getUpdatedAt());
            assertNotNull(ages.get(1).getUpdatedBy());
        }
    }
}

Upvotes: 2

Views: 2393

Answers (1)

theseadroid
theseadroid

Reputation: 471

If you look at the code of JpaRepository interface you may see this:

@NoRepositoryBean

public interface JpaRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> 

So the point is you don't want a bean for JpaRepository. Same thing for your own GenericRepository. In fact in the error message there's mentioning of this:

Error creating bean with name 'genericRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object

Add @NoRepositoryBean should work.

Upvotes: 4

Related Questions