如何在Spring Data JPA中向Query注释添加模式名称

时间:2015-12-29 10:41:53

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

目前我有这样一段代码,但这不起作用,因为我必须在查询中的每个表之前添加模式名称(如DEV.DASHBOARDS_METADATA):

public interface DashboardMetadataDao extends CrudRepository<DashboardMetadata, Integer> {

    @Query("SELECT D FROM DASHBOARDS_METADATA D " + 
           "INNER JOIN FAC_DASHBOARDS_LINK DL ON D.ID = DL.DASHBOARD_ID " +
           "INNER JOIN FIRMS F ON DL.FAC_ID = F.FAC_UNIT_ID " + 
           "INNER JOIN USERS U ON U.FIRM_ID = F.FIRM_ID WHERE LOWER(U.USERID) = LOWER(:userid)")
    public Set<DashboardMetadata> findByUserId(@Param("userid") String userId);
}

问题是模式名称因数据库而异(DEV / QA / PROD)。通常我使用组件的方法,在查询生成期间将模式的名称添加到每个表中。如何使用注释做到这一点?

谢谢!

2 个答案:

答案 0 :(得分:2)

下面的代码段对我有用

@Query(
              value = "select *\n" + 
                    "from {h-schema}TABLE-A r left join {h-schema}TABLE-B p on r.ID=p.ID\n" + 
                    "left join {h-schema}TABLE-C pe on p.ID=pe.ID\n" + 
                    "left join {h-schema}TABLE-D e on pe.ENT_ID=e.ENT_ID\n" + 
                    "where r.role_type='Provisioning'", 
              nativeQuery = true)

答案 1 :(得分:1)

Hibernate有一个变量,可以在本机查询中使用,以获取名为{h-schema}的模式名称

https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/chapters/query/native/Native.html#sql-global-catalog-schema您只需将{h-schema}table放入查询中即可。我认为你必须将它转换为原生查询。