gpt4 book ai didi

java - DAO Hibernate Java Select All rows into a collection

转载 作者:行者123 更新时间:2023-11-29 03:33:00 25 4
gpt4 key购买 nike

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

我想要做的是从一个表中选择所有行,而不是只使用一个键获取一个行。我正在为此使用 hibernate 和 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() 以返回类似集合的内容。我不知道,我现在很绝望。

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

最佳答案

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

的方法

getDao().getAll()

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

关于java - DAO Hibernate Java Select All rows into a collection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27630803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com