Spring数据和Java Generics:如何将多个存储库接口合并为一个?

时间:2014-06-05 16:47:12

标签: java spring maven generics

我现在拥有什么
我有2个存储库(实际上很多,但是这个例子可以说2),它们看起来像

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

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

所有其他存储库看起来都一样。唯一的区别是类名Age, Gender, Network

他们有类似的实现。以上两个的相应实现是

@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();
    }
}

@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();
    }
}

在存储库接口上调用完全相同的方法。

我做了什么
我创建了一个通用存储库接口

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

并且在我尝试的其中一个实现类中

@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();
    }
}

运行测试时,maven在加载应用程序上下文时失败。我看到的错误是

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

我做错了什么?

我在寻找什么?
Repositories泛化成一个,这样我就不必编写多个泛型类,因为它们在类名中有所不同。

如何解决此类问题?

更新
我的测试看起来像

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());
        }
    }
}

1 个答案:

答案 0 :(得分:3)

如果你看一下JpaRepository接口的代码,你可能会看到:

@NoRepositoryBean

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

所以关键是你不想要一个bean用于JpaRepository。您自己的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

添加@NoRepositoryBean应该可以。