spring-data-jpa 如何使用jpa存储库查询多对一Map

1sbrub3j  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(195)

我浏览了大量的堆栈溢出流来解决我的问题,尽管我得到的答案在我的应用程序中不起作用。我正在尝试根据父类中的一个字段来查询数据,该字段由@OnetoMany注解Map。
我的父类是NodeDetails:

@Entity
@Table(name = "tb_node_details")
public class NodeDetails implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 3232846606798223969L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "instance_id", nullable = false)
    private String instanceId;

    @Column(name = "host_id")
    private String hostId;

    @Column(name = "host_type")
    private String hostType;

    @Column(name = "client_name")
    private String clientName;

我有一个子类NodeAdapterKindDetails:

@Entity
@Table(name = "tb_node_adapter_kind_details")
public class NodeAdapterKindDetails implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 6542657705062870675L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "adapter_kind", nullable = false)
    private String adapterKind;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "node_id", nullable = false)
    private NodeDetails nodeDetails;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "adapterKindDetails")
    private Set<NodeAdapterInstanceDetails> adapterInstanceDetails = new HashSet<>();

它的子类是NodeAdapterInstanceDetails:

@Entity
@Table(name = "tb_node_adapter_instance_details")
public class NodeAdapterInstanceDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "adapter_instance")
    private String adapterInstance;

    @Column(name = "active_status", columnDefinition = "varchar(20) default 'not active'")
    private String activeStatus;

    // TODO: change the timestamp from string to date... Check what is going
    // wrong in the commented setTimeStamp method
    @Column(name = "time_stamp")
    private String timeStamp;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "adapter_kind_id", nullable = false)
    private NodeAdapterKindDetails adapterKindDetails;

nodeAdapterInstance类别的DAO:

@Repository(value="nodeAdapterInstanceDao")
    public interface NodeAdapterInstanceDao extends JpaRepository<NodeAdapterInstanceDetails, Long> {

        @Query("select distinct n.adapterInstance from NodeAdapterInstanceDetails n where n.adapterKindDetails.nodeDetails.clientName = :client_name")
        public List<String> getDistinctAdapterInstanceByClientName(@Param("client_name") String clientName);

        @Query("select n from NodeAdapterInstanceDetails n where n.adapterInstance = :adapter_instance and n.adapterKindDetails.nodeDetails.clientName = :client_name")
        public NodeAdapterInstanceDetails getLatestAdapterInstanceByAdapterInstanceAndClientName(@Param("adapter_instance") String adapterInstance, @Param("client_name") String clientName);

}

但当我尝试运行spring Boot 时,在应用程序启动时,我得到:

**

异常错误:创建名为“adapterKindParserServiceImpl.”的Bean时出错:通过字段“nodeAdapterInstanceDao”表示的依赖关系不满足;嵌套的异常是一个嵌套的异常。创建名为“nodeAdapterInstanceDao”的Bean时出错:调用init方法失败;嵌套的异常是组织。找不到类型NodeAdapterInstanceDetails的属性clientName!

**

我在查询中做错了什么吗?我该如何解决这个错误呢?我们可以使用JPA存储库, @query 如下:

@Query("select n from NodeAdapterInstanceDetails n where n.adapterInstance = :adapter_instance and n.adapterKindDetails.nodeDetails.clientName = :client_name")

因为我在节点适配器示例类中没有客户端信息,所以我需要从我层次结构中最大的父类(即节点细节类)中获取它。
这是我的服务:

@Service
public class AdapterKindParserServiceImpl implements NodeAdapterKindDetailsParserService {
    @Autowired
    @Qualifier(value="nodeAdapterInstanceDao")
    private NodeAdapterInstanceDao nodeAdapterInstanceDao;
    private List<String> getDistinctAdapterInstances(String clientName) {
        return nodeAdapterInstanceDao.findDistinctAdapterInstanceByClientName(clientName);
    }
}

我完整例外:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adapterKindParserServiceImpl': Unsatisfied dependency expressed through field 'nodeAdapterInstanceDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nodeAdapterInstanceDao': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property clientName found for type NodeAdapterInstanceDetails! at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] at com.vmware.supernova.TopolizerApp.main(TopolizerApp.java:18) [classes/:na] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nodeAdapterInstanceDao': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property clientName found for type NodeAdapterInstanceDetails! at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] ... 19 common frames omitted Caused by: org.springframework.data.mapping.PropertyReferenceException: No property clientName found for type NodeAdapterInstanceDetails! at org.springframework.data.mapping.PropertyPath.(PropertyPath.java:77) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.repository.query.parser.Part.(Part.java:76) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:247) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:378) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.repository.query.parser.PartTree.(PartTree.java:89) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:64) ~[spring-data-jpa-1.11.6.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103) ~[spring-data-jpa-1.11.6.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:214) ~[spring-data-jpa-1.11.6.RELEASE.jar:na] at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77) ~[spring-data-jpa-1.11.6.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java:436) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263) ~[spring-data-commons-1.13.6.RELEASE.jar:na] at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.6.RELEASE.jar:na] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] ... 29 common frames omitted

请说明如何解决此异常。如果可能,请说明使用@OnetoOne、@OnetoMany和@ManytoMany查询的更好方法。
先谢谢你

gab6jxml

gab6jxml1#

请按如下方式更正查询:

select distinct 
    n.adapterInstance 
from 
    NodeAdapterInstanceDetails n 
    join n.adapterKindDetails ad
    join ad.nodeDetails nd
where 
    nd.clientName = :client_name

select 
    n 
from 
    NodeAdapterInstanceDetails n 
    join n.adapterKindDetails ad
    join ad.nodeDetails nd
where 
    n.adapterInstance = :adapter_instance and 
    nd.clientName = :client_name

要获得使用链接实体字段的能力,您必须首先“联接”此实体。
更多信息:JPQL语言参考

相关问题