简体   繁体   English

我如何在 spring 2.2.4.RELEASE 中从类中检索 jpa-repository 实例

[英]how can i retrive jpa-repository instance from class in spring 2.2.4.RELEASE

i need to retrieve specific instance of jpa-repository from an entity class to call findAll, findOne ecc...我需要从实体类中检索 jpa-repository 的特定实例以调用 findAll、findOne ecc ...

Entity entity = getRepository(EntityClass).findOne(uriParamValue);

In the previous spring version (1.5.X) i was doing it this way:在之前的 spring 版本 (1.5.X) 中,我是这样做的:

public JpaRepository<Entity, Serializable> getRepository(Class<?> javaClass) {
    return (JpaRepository<Entity, Serializable>) repositories.getRepositoryFor(javaClass);
}

but in spring 2.2.X the Repository class interfaces has been changed from:但是在 spring 2.2.X 中,Repository 类接口已从:

public Object getRepositoryFor(Class<?> domainClass) {

to:到:

public Optional<Object> getRepositoryFor(Class<?> domainClass) {

and i can't cast "Optional getRepositoryFor(X)" to JpaRepository instance to able to call JpaRepository methods.并且我无法将“可选的 getRepositoryFor(X)”转换为 JpaRepository 实例以能够调用 JpaRepository 方法。

Where am I doing wrong?我哪里做错了?

thanks谢谢

I'm not familiar with the getRepositoryFor method, but Optional is a standard Java class intruduced in Java SE 8. You can check if the value is present and get the actual value from the Optional if it is present.我不熟悉getRepositoryFor方法,但Optional是 Java SE 8 中getRepositoryFor的标准 Java 类。您可以检查该值是否存在,如果存在,则可以从 Optional 获取实际值。 Here is a sample using orElseThrow , but there are also other variants.这是使用orElseThrow的示例,但也有其他变体。 Checkout the Javadoc.查看 Javadoc。

public JpaRepository<Entity, Serializable> getRepository(Class<?> javaClass) {
    return (JpaRepository<Entity, Serializable>) repositories.getRepositoryFor(javaClass)
           .orElseThrow(() -> new MyException());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 启动 2.2.4.RELEASE 在 Origin 被定义为 webapp 自己的主机名以外的任何内容时返回 403 获取请求 - Spring boot 2.2.4.RELEASE returning 403 for GET request when Origin is defined as anything other than the webapp's own hostname 如何使用 Spring 引导在 JPA 存储库中编写 SQL 查询? - How can I write SQL query in JPA repository with Spring Boot? JPA信息库读取访问提交外部事务 - JPA-Repository read access commits outer transaction JPA-Repository save:插入约束违反后继续保存 - JPA-Repository save: Proceed saving after insert Constraint violation 如何在 Spring Boot 中实现通用 JPA 存储库 - 它可以自动装配到任何实体/类类型的 spring 服务中 - How to implement Generic JPA Repository in Spring Boot - Which can be autowired into spring services for any entity/class type 如何从Spring Data JPA中的cusom存储库访问主存储库? - How to access main repository from cusom repository in Spring Data JPA? Spring Data JPA + Hibernate:如何使用抽象超类上的存储库进行对象检索? - Spring Data JPA + Hibernate: How do I use a repository on abstract super class for object retrieval? 如何在 mapstruct 映射器中使用 spring 注入,存储库 class? - How can I use spring injection, A repository class in a mapstruct mapper? Spring JPA Repository类中的ManyToMany产品模式中如何执行“ findByCategory”? - How to do a “findByCategory” in a ManyToMany products schema in Spring JPA Repository class? Spring 引导 JPA - 如何仅从存储库中获取用户名? - Spring Boot JPA - How can you fetch only the usernames from repository?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM