这可能类似于已经提出的其他几个问题但我无法找到我得到的错误的确切答案。我正在尝试在Spring JPA中构建泛型基类
基础界面:
@NoRepositoryBean
public interface GenericRepository <T, ID extends Serializable>
{
public T handleCustom(String id);
}
实施班:
@SuppressWarnings("serial")
public class GenericRepositoryImpl<T, ID extends Serializable>
implements GenericRepository<T, ID>
{
@PersistenceContext
private EntityManager entityManager;
Class type;
public T handleCustom(String id)
{
Annotation[] ann = type.getAnnotations();
return null;
}
}
特定于域的类:
public interface DomainRepository
extends JpaRepository<Domain, String>,
GenericRepository<Domain, String>
{
}
实体:
@Entity
public class Domain extends ParentEntity
{
@Id
private String domainId;
private String domainName;
public Domain()
{
}
//setters getters
.....
}
配置文件:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.rabbitmq.jpa" )
public class JPAConfig
{
}
以下错误:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'domainRepository':
Invocation of init method failed;
nested exception is org.springframework.data.mapping.PropertyReferenceException:
No property handleCustom found for type Domain!
我在这里做错了什么。
答案 0 :(得分:1)
自定义存储库功能的实现必须是Spring Data接口的名称+&#34; Impl&#34;而不是ypur界面的名称+&#34; Impl&#34;。 因此,在您的情况下,实现类必须是DomainRepositoryImpl而不是GenericRepositoryImpl。
请参阅弹簧文档以供参考。
答案 1 :(得分:0)
我使用以下配置工作:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.rabbitmq.jpa",repositoryBaseClass = GenericRepositoryImpl.class )
public class JPAConfig
{
}
基础界面:
@NoRepositoryBean
public interface GenericRepository <T, ID extends Serializable> extends JpaRepository<T, ID>
{
public T handleCustom(String id);
}
实施班:
@NoRepositoryBean
public class GenericRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements GenericRepository<T, ID>
{
protected final EntityManager entityManager;
public GenericRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
}
Class type;
public T handleCustom(String id)
{
Annotation[] ann = type.getAnnotations();
return null;
}
}
域特定类:
@Repository
@Transactional(readOnly = true)
public interface DomainRepository extends GenericRepository <Domain, String>
{
}