Java Spring JPA数据:自定义通用存储库和服务:未满足的PendencyException
我正在与许多实体一起开发一个restful服务。如果我们考虑两组父亲资源和儿童资源,两组成员在组范围中有相同的实施CRUD操作。 因此,每个层不仅仅只有一个泛型类。这是我的密码: 存储库: 具有所有实体使用的方法的基本存储库:Java Spring JPA数据:自定义通用存储库和服务:未满足的PendencyException,java,spring-boot,spring-data-jpa,generic-programming,Java,Spring Boot,Spring Data Jpa,Generic Programming,我正在与许多实体一起开发一个restful服务。如果我们考虑两组父亲资源和儿童资源,两组成员在组范围中有相同的实施CRUD操作。 因此,每个层不仅仅只有一个泛型类。这是我的密码: 存储库: 具有所有实体使用的方法的基本存储库: @Repository public interface GenericRepository<T, ID extends Serializable> extends JpaRepository<T, ID> { Page<T>
@Repository
public interface GenericRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
Page<T> findAll(Pageable pageable);
}
搜索了很多,但没有找到解决此问题的方法。谢谢你的帮助
通过为每个实体创建具体的存储库,我解决了这个问题。 我试图减少上课的次数。因此,我定义了3个通用存储库来完成所有其他存储库的工作,这些存储库被定义为服务于所有实体。但我明白这是不可能的,具体的存储库必须在定制存储库的最后一级定义,以便与服务层交互 原因在于反思。Spring使用反射来完成所有幕后工作,它必须知道它必须为哪个实体使用提供者(Hibernate)
需要注意的是,如果您想要创建通用服务层,它还必须在最后一级有具体的实现。因为具体存储库必须在某处声明请检查我的回答:@Cepr0谢谢。但是如果我想这样做,它将不能满足泛型的目的,因为我仍然有重复的方法来完成同样的工作,当然,我不能使用我在Repository中定义的自定义方法。如何创建自己的泛型存储库的具体实现?@SimonMartinelli我修复了它。为了使用spring存储库,必须在最后一个级别具体定义接口。请你把这个作为答案贴出来好吗。非常感谢。
@Repository
public interface EntityGenericRepository<T, ID extends Serializable> extends GenericRepository<T, ID> {
T findByName(String name);
}
@Repository
public interface NestedEntityGenericRepository<T, ID extends Serializable> extends GenericRepository<T, ID> {
Page<T> findByFatherId(ID fatherId, Pageable pageable);
}
public interface GenericService<T,ID extends Serializable> {
Page<T> findAll(int page, int size);
T findById(ID id);
}
public interface EntityGenericService<T, ID extends Serializable> extends GenericService<T, ID> {
T findByName(String name);
T save(T t);
void update(ID id, T t);
void softDelete(ID id);
}
public interface NestedEntityGenericService<T, ID extends Serializable> {
Page<T> findBySensorId(ID fatherId, int page, int size);
T save(ID fatherId, T t);
void update(ID fatherId, ID id, T t);
void softDelete(ID fatherId, ID id);
}
@Service
public class GenericServiceImpl<T,ID extends Serializable>
implements GenericService<T,ID> { //codes }
@Service
public class EntityGenericServiceImpl<T, ID extends Serializable>
extends GenericServiceImpl<T, ID>
implements EntityGenericService<T, ID> {//codes}
@Service
public class NestedEntityGenericServiceImpl<T, U, ID extends Serializable>
extends EntityGenericServiceImpl<T, ID>
implements NestedEntityGenericService<T, ID> {//codes}
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating
bean with name 'entityGenericServiceImpl': Unsatisfied dependency expressed through
field 'genericRepository': Error creating bean with name 'nestedEntityGenericRepository':
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException:
Not a managed type: class java.lang.Object; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'nestedEntityGenericRepository': Invocation of init method failed; nested exception is
java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object