在NamedNativeQuery中获取Integer.class作为结果

时间:2016-08-03 14:18:14

标签: java hibernate ejb

我有一个名为NamedNativeQuery的实体(StockKeepingUnit):

@NamedNativeQuery(name = StockKeepingUnit.NQ_FINDBY_ARTICLE_AND_SIZE, resultClass = StockKeepingUnit.class, hints = {
        @QueryHint(name = "org.hibernate.cacheable", value = "false"),
        @QueryHint(name = "org.hibernate.fetchSize", value = "1"),
        @QueryHint(name = "org.hibernate.readOnly", value = "true") }, query = "SELECT * "
            + "FROM StockKeepingUnit sku " + "WHERE sku.article = :articleNumber "
            + "AND sku.size = :size " + "AND sku.deleted = 0")

使用Java调用:

Query query = em.createNamedQuery(StockKeepingUnit.NQ_FINDBY_ARTICLE_AND_SIZE);
query.setParameter(StockKeepingUnit.NQ_PARAM_ARTICLE_NR, articleNumber);
query.setParameter(StockKeepingUnit.NQ_PARAM_SIZE, size);

return (StockKeepingUnit) query.getSingleResult();

使用此查询,我只想选择sku.stockKeepingUnitNumber,并在执行此查询时获得Integer-Value作为结果。

我尝试过类似的东西,但这不起作用......

@NamedNativeQuery(name = StockKeepingUnit.NQ_FINDBY_ARTICLE_AND_SIZE, resultClass = Integer.class, hints = {
        @QueryHint(name = "org.hibernate.cacheable", value = "false"),
        @QueryHint(name = "org.hibernate.fetchSize", value = "1"),
        @QueryHint(name = "org.hibernate.readOnly", value = "true") }, query = "SELECT sku.stockKeepingUnitNumber "
            + "FROM StockKeepingUnit sku " + "WHERE sku.article = :articleNumber "
            + "AND sku.size = :size " + "AND sku.deleted = 0")

使用Java调用:

Query query = em.createNamedQuery(StockKeepingUnit.NQ_FINDBY_ARTICLE_AND_SIZE);
query.setParameter(StockKeepingUnit.NQ_PARAM_ARTICLE_NR, articleNumber);
query.setParameter(StockKeepingUnit.NQ_PARAM_SIZE, size);

return (Integer) query.getSingleResult();

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

我用ResultSetMapping解决了问题: - )

private void saveBitmap(Bitmap bmp, String fileNameHigh, String fileNameLow, int resolutionHigh, int resolutionLow, String resolutionQuality) {

        File f = new File(Environment.getExternalStorageDirectory() + "FiltureImages");
        if (!f.exists()) {
            f.mkdirs();
        }
        File file = new File(f, fileNameHigh + resolutionQuality + ".png");
        if (file.exists()) {
            file.delete();
        }
        File file1 = new File(f, fileNameLow + resolutionQuality + ".png");
        if (file1.exists()) {
            file1.delete();
        }
        try {
            FileOutputStream high = new FileOutputStream(file);
            FileOutputStream low = new FileOutputStream(file1);

            bmp.compress(Bitmap.CompressFormat.JPEG, resolutionHigh, high);
            bmp.compress(Bitmap.CompressFormat.JPEG, resolutionLow, low);
            Toast.makeText(this, "Image save successfully", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(this, "Image save falid", Toast.LENGTH_SHORT).show();
        }
    }
在NamedQuery中,您可以使用属性

来引用此映射
@SqlResultSetMapping(name = StockKeepingUnit.NQ_FIND_ONLY_SKU, columns = {
    @ColumnResult(name = StockKeepingUnit.NQ_RESULT_SKU_NAME)}