Oracle:ORA-00911:无效字符

时间:2017-01-25 16:15:24

标签: java string oracle hibernate nativequery

在我的代码中,我有以下查询字符串:

   private static final String QUERY = format(
                    "  SELECT t2.address " +
                    "  FROM schema.table1 t1    ," +
                    "  schema.table2 t2 ," +
                    "  schema.table3 t3            ,"+
                    "  schema.table4 t4 " +
                    "  WHERE t2.uniqueIdentifier =:%s " +
                    "  AND  t1.parent_id = t2.parent_alias " +
                    "  AND t3.company_id  = t1.company_id " +
                    "  AND t3.record_age  = t2.recordAge " +
                    "  AND t2.name = 'stubName' " +
                    "  AND t4.pln_foi_id = t2.recordAge ",uniqueIdentifier);

在本机查询中调用的内容如下:

 public String getAddress(String uniqueIdentifier){

        String result = null;

        try {
            Query query = persistence.entityManager().createNativeQuery(QUERY);
            query.setParameter("uniqueIdentifier", uniqueIdentifier);
            result = query.getSingleResult();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

当我测试此查询时,我得到以下内容:

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Caused by: java.sql.SQLSyntaxErrorException: ORA-00911: invalid character

可能导致此错误的原因是什么?我在查询字符串或代码中看不到可能导致它的任何问题。

1 个答案:

答案 0 :(得分:2)

查询应为

<Viewbox>
    <Border BorderThickness="3" BorderBrush="Red">
     <Image Stretch="None" ></Image>
    </Border>
   </Viewbox>

并移除对... " WHERE t2.uniqueIdentifier = :uniqueIdentifier " ... 的调用;根据第一个String.format()变量的值,您将要么接受SQL注入,要么uniqueIdentifier将无效。

说明:如果使用带参数的本机查询,则需要在查询中使用setParameter()(冒号)前缀指定参数的名称。要使用参数:,请在查询中添加foo并调用:foo以指定应使用哪个值代替参数。