【问题标题】:DAO Hibernate Java Select All rows into a collectionDAO Hibernate Java 选择所有行到一个集合中
【发布时间】:2014-12-24 02:50:59
【问题描述】:

所以,基本上我已经在同一个过程中停留了大约 1 周,我想我基本上已经失去了它。我一直在做和分配,我无法更改已经构建的内容,但我需要使用那里的内容来获得我的结果。

我想要做的是从一个表中选择所有行,而不是只用一个键获取一个。我正在使用休眠和DAO。我现在只选择 1 个查询启动并运行,但我面临着获取所有记录并将它们保存为列表或集合。

public CachedObject get(String key) {
    CachedObject rtnValue = null;

    log.debug("Entering get(key) for key = " + key + "...");

    // try to find entry from db as long as key
    // is not in the do not cache list
    if (!getDoNotCacheKeys().contains(key)) {
        try {
            rtnValue = getDao().find( getFetchQueryName(), KEY_PARM_NAME, key);
            // if we got an object back - and it has not expired - reset
            // its last active timestamp value to now and save
            if (rtnValue != null && !rtnValue.hasExpired(getMaxIdle(), getMaxAge())) {
                log.debug("Cache entry being updated with current timestamp...");
                rtnValue.setLastActive(new Timestamp(System.currentTimeMillis()));
                getDao().saveOrUpdate(rtnValue);
            }
        }
        catch (DatabaseException exc) {
            log.error("Exception triggered on get of cache entry with key = " + key, exc);
            if (exc.isSerializationException()) {
                try {
                    log.warn("Serialization of old value triggered exception - proceeding to remove obsolete record for key = " + key + "...");
                    int numRows = getDao().delete(getDeleteQueryName(), KEY_PARM_NAME, key);
                    log.info("The delete for key = " + key + " successfully deleted " + numRows + " rows in the db...");
                }
                catch (DatabaseException exc2) {
                    log.error("Unable to remove record for key " + key + " from the cache db...", exc2);
                }
            }
        }            
    }
    else {
        log.info("Key " + key + " found in do not cache list - no lookup performed...");
    }
    log.debug("Exiting get(key) for key = " + key + "...");

    return rtnValue;
}

然后用于将其添加到缓存中

public CachedObject add(String key, CachedObject obj) {
    CachedObject rtnValue = obj;

    log.debug("Entering add(key, obj) for key = " + key + "...");

    // add to the cache as long as the key value is 
    // not in the do not cache list
    if (!getDoNotCacheKeys().contains(key)) {
        // update timestamp values
        long now = System.currentTimeMillis();
        obj.setLastActive(new Timestamp(now));
        obj.setFirstActive(new Timestamp(now));

        // persist obj to cache
        try {
            log.debug("Saving cache object to the db...");
            getDao().saveOrUpdate(obj);
        }
        // if object with same key is already in db - then it must
        // have been added after check or has expired - so just perform get to
        // read it in 
        catch (ConstraintViolationException exc) {
            log.warn("Existing cache object with key = " + key + " found in the database. Attempting to update existing copy via get()...");
            CachedObject dbCopy = get(key);
            if (dbCopy == null) {
                log.error("Unexpected null returned on get of existing cache entry with key = " + key + " !!!");                    
            }
            // now replace serialized data just in case the class
            // has changed and won't deserialize properly
            // also copy over last and first active timestamps
            // since 
            dbCopy.setCacheObject(obj.getCacheObject());
            dbCopy.setLastActive(obj.getLastActive());
            dbCopy.setFirstActive(obj.getFirstActive());
            try {
                getDao().saveOrUpdate(dbCopy);
            }
            catch (DatabaseException exc2) {
                log.error("Unexpected error triggered while updating existing cached object with new serialized content...", exc2);
            }
            // set return to return the db based copy and not the original
            // passed into this method
            rtnValue = dbCopy;

        }
        catch (DatabaseException exc) {
            log.error("Exception triggered on save of cache entry with key = " + key + ": " + obj, exc );
        }            
    }
    else {
        log.info("Key " + key + " found in do not cache list - no add performed...");
    }

    log.debug("Exiting add(key, obj) for key = " + key + "...");

    return rtnValue;
}

我现在想要做的是现在尝试选择所有记录并将它们保存在一个集合中,以便我以后可以使用它们进行验证(仍然需要这样做),但事实上我没有非常了解如何使用 DAO 选择所有记录。

find() 方法很简单

CachedObject find(String queryName, String keyParmName, Object key) throws DatabaseException;

我在想类似的东西

ArrayList<CachedObject> listObj = getDao().find(fetchAllQueryName, null, getDao());

但是,更改 find() 以返回类似集合的内容。我不知道,我现在很绝望。

任何帮助将不胜感激, 谢谢,圣诞快乐。

【问题讨论】:

    标签: java mysql spring hibernate dao


    【解决方案1】:

    dandalf 是对的,你必须创建一个类似

    的方法

    getDao().getAll()

    getDao().findAll() 正如您在评论中提到的,此方法不是从 AbstractDao 继承的,那么显然您将签名保留在 AbstractDao 作为抽象方法,随着功能的增加,引入了不同的方法,您不能仅通过预定义的方法完成所有操作。

    【讨论】:

    • 这似乎是正确的方法,我不知道是否应该在每个中创建它是继承的。例如查找,有这两个... CachedObject find(String queryName, String keyParmName, Object key) throws DatabaseException;--- public CachedObject find(String queryName, String keyParmName, Object key) throws DatabaseException { return (CachedObject) myDao.find(queryName, new String[] {keyParmName}, new Object[] {key}); }
    • 我是否应该为此创建相同的但对于 findAll,显然返回一个集合(或列表),因为它应该是这样的。
    • 是的,你是对的,在你的 AbstractDao 中创建一个抽象方法,所有继承的类都会继承它们,如果你的一个类现在需要功能,它们实现该功能,而你的另一个类现在不需要它只需给它简单的主体 {} 返回 null,但在某一时刻,您也需要其他类中的该功能,因为这就是为什么要制作通用抽象类的原因:)
    • 是的,我知道这个概念,但第一次使用hibernate和DAO,所以迷失了一些定义。但是猜猜它是否像 find 那样工作,应该为 findAll 复制它,但使用 Collection 作为返回。最后一个问题,考虑到来自 DAO 的对象,我应该使用集合还是 ArrayList?
    • 实际上我没有经过身份验证的资源来回答这个问题,但我曾经这样做我保留 returnType 一个父类,如List 并返回子类Arraylist
    猜你喜欢
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多