【问题标题】:Is it possible to create common JpaRepository Interface?是否可以创建通用 JpaRepository 接口?
【发布时间】:2019-03-19 13:05:02
【问题描述】:

我已经创建了一个这样的界面,

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

和服务等级

@Service
public class LOVService<T>{
    @Autowired
    private IJpaRepositoryCustom<T> jpaRepositoryCustom;
}

但是使用上面的代码,我得到了一个异常

严重 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.core.StandardContext.listenerStart 异常 将上下文初始化事件发送到类的侦听器实例 [org.springframework.web.context.ContextLoaderListener] org.springframework.beans.factory.BeanCreationException:错误 创建名为“IJpaRepositoryCustom”的 bean:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException: 不是托管类型:类 java.lang.Object 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)

还有其他方法可以创建通用 JPA 存储库吗?

提前致谢。

【问题讨论】:

标签: hibernate spring-mvc generics spring-data-jpa


【解决方案1】:

当我们想将自定义方法添加到所有 Spring Data JPA 存储库中时,我们要做的第一件事就是创建一个声明自定义方法的基本接口。

1) 第一步:创建基础接口

    //Annotation to exclude repository interfaces from being picked up and thus in consequence getting an instance being created.
@NoRepositoryBean  
public interface ReadOnlyRepository<T, ID extends Serializable> extends Repository<T, ID> {

    T findOne(ID id);

    List<T> findAll();

}

2) 第二步:实现基础存储库接口

public interface IUserReadOnlyRepository extends ReadOnlyRepository<User,Integer> 

{
    User findById(Integer id);

    List<User> findByName(String name);

    @Query("select u from MyUser u where u.name = ?1")
    List<User> findUserByAttributeAndValueCustomQuery(String string2);   
}

3) 第三步:自动连接到 Controller/RESTController

  @RestController
    @RequestMapping("/user")
    @CrossOrigin
    //@EnableTransactionManagement
    public class UserController {

        @Autowired
        private IUserReadOnlyRepository userReadOnlyRepository;

    @RequestMapping(value = "/getUserByIdReadOnlyRepository/{id}")
    public @ResponseBody User getUserById(@PathVariable Integer id)
    { 
        User user = userReadOnlyRepository.findById(id);
        return user;

    }

注意:它非常适合我!如果需要任何帮助,请联系我。

【讨论】:

  • 感谢您的回复 是的,这是将自定义方法添加到所有 Spring Data JPA 存储库的完美示例,但我的问题不同。在您的示例中,您必须为每个实体创建多个存储库接口。但我想为所有实体创建公共存储库。
  • @KalanaWeerarathne 为所有实体建立一个共同的仓库并不是一个好主意。请考虑 SRP。
【解决方案2】:

您可以像这样编辑您的服务:

@Service
public class LOVService{

    @Autowired
    private IJpaRepositoryCustom jpaRepositoryCustom;
}

还有你的回购:

@Repository
public interface IJpaRepositoryCustom extends JpaRepository<YourEntityClass,Long>{
}

如果您要自动装配,则无需将参数传递给存储库。

我希望这会有所帮助。

【讨论】:

  • @AngadBansode,我很高兴,我能帮上忙。如果您发现这可行,您也可以投票并接受此答案。谢谢
  • 感谢回复这个我试过了,但是@Repository public interface IJpaRepositoryCustom extends JpaRepository{ }有编译错误
  • @PawanTiwari 错误:(11, 61) java: 找不到符号 symbol: class T
  • @KalanaWeerarathne,您只需提及您的实体类或 DTO 类即可获得结果。或者你可以简单地在那里传递对象。我也编辑了答案。
  • 亲爱的@PawanTiwari 可以使用特定的实体类。我想为所有实体类创建通用 JpaRepository
猜你喜欢
  • 1970-01-01
  • 2020-08-26
  • 2011-03-11
  • 1970-01-01
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多