【问题标题】:Join Query for unrelated entities in Spring boot JPA在 Spring Boot JPA 中加入不相关实体的查询
【发布时间】:2017-04-30 13:27:10
【问题描述】:

我正在尝试对不相关的实体 A 和 B 编写如下查询

Select a.x,a.y,a.z FROM A a ,B b Where
b.Parameter_Name='APPLICABLE_RULE_TYPE' And b.Parameter_KEY='MC' AND 
b.Parameter_Value=a.rule_type And a.Rule_Status='ON' Order By Rule_Priority;

我无法弄清楚这应该如何写在 Spring 引导应用程序的 Repository 类中。

有人可以建议一种方法吗

提前致谢。

编辑:

我已经尝试将其作为原生查询,例如

@Query(value = "Select Rule_Id, Rule_Sql,S_System,S_Entity, s_table_name,Rule_Sql_Type "
        + "FROM rule_master TRM ,T_SYSTEM_CONFIG TSC  Where"
        + " Tsc.Parameter_Name='APPLICABLE_RULE_TYPE' "
        + " And Tsc.Parameter_KEY= :systemType"
        + " AND Tsc.Parameter_Value=trm.rule_type  "
        + " And Rule_Status='ON'"
        + " Order By Rule_Priority", nativeQuery = true)
public List<RuleMaster> findByRuleStatus(@Param("systemType" String systemType);

但出现此错误

o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 17006, SQLState: null
o.h.engine.jdbc.spi.SqlExceptionHelper   : Invalid column name
org.springframework.orm.jpa.JpaSystemException: could not execute query; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query

【问题讨论】:

  • 我猜你正试图从你的存储库中只提取特定的列。在这里查看这个答案stackoverflow.com/questions/24710626/…
  • 我在查询中有这个条件 b.Parameter_Value=a.rule_type 这就是为什么我要求加入不相关的实体。

标签: java hibernate spring-boot spring-data-jpa


【解决方案1】:

我能够通过使用本机查询本身来解决这个问题。

错误是因为我只从表中选择了部分列。相反,我使用了以下工作正常的查询:

@Query(value = "Select * "
    + "FROM rule_master TRM ,T_SYSTEM_CONFIG TSC  Where"
    + " Tsc.Parameter_Name='APPLICABLE_RULE_TYPE' "
    + " And Tsc.Parameter_KEY= :systemType"
    + " AND Tsc.Parameter_Value=trm.rule_type  "
    + " And Rule_Status='ON'"
    + " Order By Rule_Priority", nativeQuery = true)
public List<RuleMaster> findByRuleStatus(@Param("systemType" String systemType);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2019-03-14
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    相关资源
    最近更新 更多