@Override
  public void addNamedQuery(String name, Query query) {
    validateNotClosed();

    // NOTE : we use Query#unwrap here (rather than direct type checking) to account for possibly
    // wrapped
    // query implementations

    // first, handle StoredProcedureQuery
    try {
      final StoredProcedureQueryImpl unwrapped = query.unwrap(StoredProcedureQueryImpl.class);
      if (unwrapped != null) {
        addNamedStoredProcedureQuery(name, unwrapped);
        return;
      }
    } catch (PersistenceException ignore) {
      // this means 'query' is not a StoredProcedureQueryImpl
    }

    // then try as a native-SQL or JPQL query
    try {
      final HibernateQuery unwrapped = query.unwrap(HibernateQuery.class);
      if (unwrapped != null) {
        // create and register the proper NamedQueryDefinition...
        final org.hibernate.Query hibernateQuery = ((HibernateQuery) query).getHibernateQuery();
        if (org.hibernate.SQLQuery.class.isInstance(hibernateQuery)) {
          sessionFactory.registerNamedSQLQueryDefinition(
              name, extractSqlQueryDefinition((org.hibernate.SQLQuery) hibernateQuery, name));
        } else {
          sessionFactory.registerNamedQueryDefinition(
              name, extractHqlQueryDefinition(hibernateQuery, name));
        }
        return;
      }
    } catch (PersistenceException ignore) {
      // this means 'query' is not a native-SQL or JPQL query
    }

    // if we get here, we are unsure how to properly unwrap the incoming query to extract the needed
    // information
    throw new PersistenceException(
        String.format(
            "Unsure how to how to properly unwrap given Query [%s] as basis for named query",
            query));
  }
예제 #2
0
  @SuppressWarnings("unchecked")
  public List<EventDto> findAllEvent(
      UserInfo userInfo,
      Boolean publicFlag,
      Boolean partyCodeFlag,
      Boolean roleFlag,
      boolean past) {
    logger.infoCode("I0001");
    String partyCode = null;
    String roleCode = null;

    StringBuilder sb = new StringBuilder();
    sb.append("SELECT");
    sb.append("    A.EVENT_KEY");
    sb.append("  , A.EVENT_TITLE");
    sb.append("  , A.EVENT_SEND_DATE");
    sb.append("  , A.EVENT_RECRUIT");
    sb.append("  , A.EVENT_TELNO");
    sb.append("  , A.PARTY_CODE");
    sb.append("  , A.PARTY_NAME");
    sb.append("  , A.EVENT_START_DATE");
    sb.append("  , A.EVENT_PLACE");
    sb.append("  , A.EVENT_MEMO");
    sb.append("  , A.EVENT_UNIT");
    sb.append("  , A.SUBJECT_INS_KBN");
    sb.append("  , A.PUBLIC_FLAG");
    sb.append("  , A.UPD_DATE");
    sb.append("  , A.UPD_USER_KEY");
    sb.append("  , ARRAY_TO_STRING(ARRAY_AGG(C.PARTY_NAME), ',') AS PARTY_NAME");
    sb.append("  , ARRAY_TO_STRING(ARRAY_AGG(C.PARTY_NAME_EN), ',') AS PARTY_NAME_EN");
    sb.append(" FROM");
    sb.append("  EV_EVENT_TBL A ");
    sb.append("  INNER JOIN EV_EVENT_PUBLIC_TBL B ");
    sb.append("    ON A.EVENT_KEY = B.EVENT_KEY ");

    sb.append("  INNER JOIN MS_PARTY_TBL C ");
    sb.append("    ON B.PARTY_CODE = C.PARTY_CODE ");
    sb.append(" WHERE");

    if (past) {
      sb.append("   CURRENT_DATE < A.EVENT_START_DATE ");
    } else {
      sb.append("  CURRENT_DATE >= A.EVENT_START_DATE ");
      sb.append("  AND CURRENT_DATE <= A.EVENT_SEND_DATE ");
    }

    if (publicFlag) {
      sb.append("  AND A.PUBLIC_FLAG = '1' ");
    }

    if (partyCodeFlag) {
      sb.append("  AND  (B.PARTY_CODE = :partyCode ");
      sb.append("    OR");
      sb.append("    A.PARTY_CODE = :partyCode )");
    }

    if (roleFlag) {
      sb.append("  AND  (B.ROLE = :role ");
      sb.append("    OR");
      sb.append("    A.PARTY_CODE = :partyCode )");
    }

    sb.append(" GROUP BY");
    sb.append("    A.EVENT_KEY");
    sb.append("  , A.EVENT_TITLE");
    sb.append("  , A.EVENT_SEND_DATE");
    sb.append("  , A.EVENT_RECRUIT");
    sb.append("  , A.EVENT_TELNO");
    sb.append("  , A.PARTY_CODE");
    sb.append("  , A.PARTY_NAME");
    sb.append("  , A.EVENT_START_DATE");
    sb.append("  , A.EVENT_PLACE");
    sb.append("  , A.EVENT_MEMO");
    sb.append("  , A.EVENT_UNIT");
    sb.append("  , A.SUBJECT_INS_KBN");
    sb.append("  , A.PUBLIC_FLAG");
    sb.append("  , A.UPD_DATE");
    sb.append("  , A.UPD_USER_KEY");
    sb.append(" ORDER BY");
    sb.append("  A.EVENT_SEND_DATE DESC");
    String sql = sb.toString();

    Query query = entityManager.createNativeQuery(sql);
    query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    if (partyCodeFlag) {
      query.setParameter("partyCode", userInfo.getTargetPartyCode());
    }
    if (roleFlag) {
      query.setParameter("partyCode", userInfo.getTargetPartyCode());
      query.setParameter("role", userInfo.getTargetRole());
    }

    List<EventDto> list = new ArrayList<>();
    List<Map> resultList = new ArrayList<>();
    resultList = query.getResultList();

    for (int i = 0; i < resultList.size(); i++) {
      EventDto dto = new EventDto();
      dto = (EventDto) objectUtil.setMapCopyValue(dto, resultList.get(i));
      list.add(dto);
    }

    logger.infoCode("I0002");
    return list;
  }