DAO Hibernate Java选择集合中的所有行
所以,基本上我已经被困在同一个过程中一个星期左右,我想我基本上失去了它。我一直在做和分配我不能改变已经在建的东西,但我需要使用那里的东西来得到我的结果 我要做的是从一个表中选择所有行,而不是只获取一个带有键的行。我正在使用hibernate和DAO。我现在已经启动并运行了selectjust1查询,但我面临着获取所有记录并将它们保存为列表或集合的问题DAO Hibernate Java选择集合中的所有行,java,mysql,spring,hibernate,dao,Java,Mysql,Spring,Hibernate,Dao,所以,基本上我已经被困在同一个过程中一个星期左右,我想我基本上失去了它。我一直在做和分配我不能改变已经在建的东西,但我需要使用那里的东西来得到我的结果 我要做的是从一个表中选择所有行,而不是只获取一个带有键的行。我正在使用hibernate和DAO。我现在已经启动并运行了selectjust1查询,但我面临着获取所有记录并将它们保存为列表或集合的问题 public CachedObject get(String key) { CachedObject rtnValue = null;
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());
arraylistlistobj=getDao().find(fetchAllQueryName,null,getDao());
但是,更改find()以返回类似于集合的内容。我不知道,在这一点上我很失望
任何帮助都将不胜感激,
谢谢,圣诞快乐。dandalf是对的,你必须创建一个类似
ArrayList<CachedObject> listObj = getDao().find(fetchAllQueryName, null, getDao());
getDao().getAll()
或
getDao().findAll()
正如您在评论中提到的,这个方法不是从AbstractDao继承的,那么很明显,您必须在
AbstractDao
中保留签名作为一个抽象方法,随着功能的增加,引入了不同的方法,您不可能只通过预定义的方法来完成所有事情 这似乎是正确的方法,我不知道的是,我是否应该在每一个继承的地方创造。例如,对于find,有以下两个。。。CachedObject find(String queryName、String keyParmName、Object key)抛出DatabaseException;——public CachedObject find(String queryName,String keyParmName,Object key)抛出DatabaseException{return(CachedObject)myDao.find(queryName,new String[]{keyParmName},new Object[]{key});}除了findAll之外,我是否应该为它创建相同的对象,显然是返回一个集合(或列表)是的,你是对的,在你的AbstractDao中创建一个抽象方法,所有继承类都将继承它们,如果你的一个类现在需要功能,它们实现了这个功能,而你的另一个类现在不需要它,那么只需给它一个简单的body{}
返回null,但在某一点上,您也需要在其他类中使用该功能,因为这就是创建泛型抽象类的原因:)是的,我知道这个概念,但第一次使用hibernate和DAO,所以在一些定义上迷失了方向。但是猜猜如果find的工作方式是这样的,那么应该为findAll复制它,但使用集合作为返回。最后一个问题,考虑到DAO中的对象,我应该使用集合还是ArrayList?事实上我没有经过身份验证的资源来回答这个问题,但我使用它时会保持returnType父类,如List
,并返回子类ArrayList