|
From: snpe <sn...@sn...> - 2004-05-16 22:56:05
|
Hello,
I want DAO with specific data access operations (base hibernate operations =
like load,find,get,save, delete ..), but I want
create object declarative (query operations like find don't important - it =
is possible to use them from HibernateTemplate)
btw for CRUD client I need DAO object with load,find,get,save, delete opera=
tions - Can I do with HibernateTemplate ?
I send example creating such object with declaration in spring context.I ma=
ke only persistent object and declaration in spring context.
If I need client with CRUD operations then I create this object only - for =
specific bussines operations I have to make added object.
I want add yet one operation in HibernateTemplate (I can do it in my BaseHi=
bernateObject) like :
public List findByValueObject(final Class entityClass,Object obj) throws Da=
taAccessException {
return executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria =3D createCriteria(session, entityClass).add(Example.=
create(obj));
return criteria.list();
}
});
}
and I can do this :
Cat cat =3D new Cat();
cat.setSex('F');
List femaleCats =3D findByValueObject(Cat.class,cat)
for list female cats - findByValueObject work with one or more properties.
or I can do query for any property of persistent class
In my Dao (with BaseHibernateDao) I do this:
Cat cat =3D (Cat) ctx.getBean('CatDao');
cat.setSex('F');
List femaleCats =3D ...
In my client user populate any properties and I do search all objects with=
this properties
=2D findByValueBean is like this, but I haven't to make query and value bea=
n.
regards
On Sunday 16 May 2004 03:42 pm, j=FCrgen h=F6ller [werk3AT] wrote:
> I don't really understand what you're trying to achieve.
> =20
> If you want to have a generic way to persist any sort of object, use a Hi=
bernateTemplate: The HibernateOperations interface that it implements speci=
fies all sorts of generic data access operations for Hibernate. You can use=
HibernateTemplate directly in your business objects, effectively abandonin=
g DAOs in your application.
> =20
> On the other hand, if you want to have DAOs with specific data access ope=
rations for a certain set of persistent objects, you should stick to a well=
=2Ddefined set of operations for those particular objects rather than offer=
all of Hibernate's data access operations. Well-defined persistent objects=
will offer specific query possibilities etc; their DAO interfaces will con=
sequently use specific query parameters rather than general HQL strings.
> =20
> In essence, use HibernateTemplate directly for generic data access (any p=
ersistent object, any HQL string), and custom DAO implementations for speci=
fic data access (specific persistent objects, specific query parameters).
> =20
> Juergen
> =20
>=20
> ________________________________
>=20
> Von: spr...@li... im Auftrag von=
snpe
> Gesendet: Do 06.05.2004 18:26
> An: spr...@li...
> Betreff: Re: [Springframework-developer] Hibernate Dao implementation (id=
ea)
>=20
>=20
>=20
> I have this error (methods with argument permanent object, not permanent =
class) : load, find work, but save, delete no)
> Next version is tested and work all methods
> I declare CatDaoImpl like :
> bean id=3D"catDao" class=3D"org.springframework.orm.hibernate.support.Bas=
eHibernateDao">
> <property name=3D"sessionFactory"><ref local=3D"sessionFa=
ctory"/>
> </property>
> <property name=3D"clazz">
> <value>eg.Cat</value>
> </property>
> </bean>
>=20
> and use like
> BaseHibernateDao catDao =3D (BaseHibernateDao) ctx.getBean("catDao");
> List cats =3D (List) catDao.loadAll();
> Long id =3D new Long(3);
> Cat cat =3D (Cat) catDao.load(id);
> etc
> We can change Dao on the fly (baseDao haven't to have clazz property) :
> BaseHibernateDao baseDao =3D (BaseHibernateDao) ctx.getBean("baseDao");
> baseDao =3D baseDao.setClazz(Dog.class);
> List dogs =3D (List) baseDao.loadAll();
> ...
>=20
> I can extends BaseHibernateDao and add new business methods (like Example=
Criteria)
>=20
> It is possible exclude all named query method in helper class - it don't =
depends from permanent class
>=20
> Regards
>=20
> Code :
>=20
>=20
> package org.springframework.orm.hibernate.support;
>=20
> import java.io.Serializable;
> import java.util.Collection;
> import java.util.List;
>=20
> import net.sf.hibernate.LockMode;
> import net.sf.hibernate.type.Type;
>=20
> import org.springframework.dao.DataAccessException;
> import org.springframework.orm.hibernate.HibernateCallback;
>=20
>=20
> public class BaseHibernateDao extends HibernateDaoSupport implements IBa=
seHibernateDao {
> =20
> private Class clazz;
> =20
> public void delete(Object obj,LockMode lockMode)
> throws DataAccessException {
> getHibernateTemplate().delete(obj,lockMode);
> }
> public void delete(Object obj) throws DataAccessException {
> getHibernateTemplate().delete(obj);
> }
> public void deleteAll(Collection entities) throws DataAccessExcep=
tion {
> getHibernateTemplate().deleteAll(entities);
> }
> =20
> public void evict(Object obj) throws DataAccessException {
> getHibernateTemplate().evict(obj);
> }
> public Object execute(HibernateCallback action) throws DataAccess=
Exception {
> return getHibernateTemplate().execute(action);
> }
> =20
> public List executeFind(HibernateCallback action)
> throws DataAccessException {
> return getHibernateTemplate().executeFind(action);
> }
> =20
> public List find(String queryString, Type type)
> throws DataAccessException {
> return getHibernateTemplate().find(queryString,clazz,type=
);
> }
> =20
> public List find(String queryString)
> throws DataAccessException {
> return getHibernateTemplate().find(queryString,clazz);
> }
> =20
> public List find(String queryString, Object[] values, Type[] type=
s)
> throws DataAccessException {
> return getHibernateTemplate().find(queryString,values,typ=
es);
> }
> =20
> public List find(String queryString, Object[] values)
> throws DataAccessException {
> return getHibernateTemplate().find(queryString,values);
> }
> =20
> public List findByNamedQuery(String queryName, Object value, Type=
type)
> throws DataAccessException {
> return getHibernateTemplate().findByNamedQuery(queryName,=
value,type);
> }
> public List findByNamedQuery(String queryName, Object value)
> throws DataAccessException {
> return getHibernateTemplate().findByNamedQuery(queryName,=
value);
> }
> =20
> public List findByNamedQuery(String queryName, Object[] values, T=
ype[] types)
> throws DataAccessException {
> return getHibernateTemplate().findByNamedQuery(queryName,=
values,types);
> }
> =20
> public List findByNamedQuery(String queryName, Object[] values)
> throws DataAccessException {
> return getHibernateTemplate().findByNamedQuery(queryName,=
values);
> }
> =20
> public List findByNamedQuery(String queryName, String paramName,
> Object value, Type type) throws DataAccessExcepti=
on {
> return getHibernateTemplate().findByNamedQuery(queryName,=
paramName,value,type);
> }
> =20
> public List findByNamedQuery(String queryName, String paramName,
> Object value) throws DataAccessException {
> return getHibernateTemplate().findByNamedQuery(queryName,=
paramName, value);
> }
> =20
> public List findByNamedQuery(String queryName, String[] paramName=
s,
> Object[] values, Type[] types) throws DataAccessE=
xception {
> return getHibernateTemplate().findByNamedQuery(queryName,=
paramNames, values,types);
> }
> =20
> public List findByNamedQuery(String queryName, String[] paramName=
s,
> Object[] values) throws DataAccessException {
> return getHibernateTemplate().findByNamedQuery(queryName,=
paramNames, values);
> }
> =20
> public List findByNamedQuery(String queryName) throws DataAccessE=
xception {
> return getHibernateTemplate().findByNamedQuery(queryName);
> }
> =20
> public List findByNamedQueryAndValueBean(String queryName, Object=
valueBean)
> throws DataAccessException {
> return getHibernateTemplate().findByNamedQueryAndValueBea=
n(queryName,valueBean);
> }
> =20
> public List findByValueBean(String queryString, Object valueBean)
> throws DataAccessException {
> return getHibernateTemplate().findByValueBean(queryString=
,valueBean);
> }
> =20
> public Object get(Serializable id, LockMode lockMode)
> throws DataAccessException {
> return getHibernateTemplate().get(clazz,id,lockMode);
> }
> =20
> public Object get( Serializable id)
> throws DataAccessException {
> return getHibernateTemplate().get(clazz,id);
> }
> =20
> public Object load( Serializable id, LockMode lockMode)
> throws DataAccessException {
> return getHibernateTemplate().load(clazz,id,lockMode);
> }
> =20
> public Object load(Serializable id)
> throws DataAccessException {
> return getHibernateTemplate().load(clazz,id);
> }
> =20
> public List loadAll() throws DataAccessException {
> return getHibernateTemplate().loadAll(clazz);
> }
> =20
> public void lock(Object obj,LockMode lockMode)
> throws DataAccessException {
> getHibernateTemplate().lock(obj,lockMode);
> }
> =20
> public void save(Object obj,Serializable id) throws DataAccessExc=
eption {
> getHibernateTemplate().save(obj,id);
> }
> =20
> public Serializable save(Object obj) throws DataAccessException {
> return getHibernateTemplate().save(obj);
> }
> =20
> public void saveOrUpdate(Object obj) throws DataAccessException {
> getHibernateTemplate().saveOrUpdate(obj);
> }
> =20
> public Object saveOrUpdateCopy(Object obj) throws DataAccessExcep=
tion {
> return getHibernateTemplate().saveOrUpdateCopy(obj);
> }
> =20
> public void update(Object obj,LockMode lockMode)
> throws DataAccessException {
> getHibernateTemplate().update(obj,lockMode);
> }
> =20
> public void update(Object obj) throws DataAccessException {
> getHibernateTemplate().update(obj);
> }
> =20
> public BaseHibernateDao(Class clazz) {
> super();
> this.clazz =3D clazz;
> }
> =20
> public BaseHibernateDao () {
> super();
> }
> =20
> public Class getClazz() {
> return clazz;
> }
> =20
> public void setClazz(Class clazz) {
> this.clazz =3D clazz;
> }
> }
>=20
> /*
> * Created on May 6, 2004
> *
> */
> package org.springframework.orm.hibernate.support;
> import java.io.Serializable;
> import java.util.Collection;
> import java.util.List;
> import net.sf.hibernate.LockMode;
> import net.sf.hibernate.type.Type;
> import org.springframework.dao.DataAccessException;
> import org.springframework.orm.hibernate.HibernateCallback;
> /**
> * @author Haris Peco
> *
> * Snpe Informacioni sistemi
> */
> public interface IBaseHibernateDao {
> public abstract void delete(Object obj,LockMode lockMode) throws =
DataAccessException;
> public abstract void delete(Object Obj) throws DataAccessExceptio=
n;
> public abstract void deleteAll(Collection entities)
> throws DataAccessException;
> public abstract void evict(Object obj) throws DataAccessException;
> public abstract Object execute(HibernateCallback action)
> throws DataAccessException;
> public abstract List executeFind(HibernateCallback action)
> throws DataAccessException;
> public abstract List find(String queryString, Type type)
> throws DataAccessException;
> public abstract List find(String queryString) throws DataAccessEx=
ception;
> public abstract List find(String queryString, Object[] values, Ty=
pe[] types)
> throws DataAccessException;
> public abstract List find(String queryString, Object[] values)
> throws DataAccessException;
> public abstract List findByNamedQuery(String queryName, Object va=
lue,
> Type type) throws DataAccessException;
> public abstract List findByNamedQuery(String queryName, Object va=
lue)
> throws DataAccessException;
> public abstract List findByNamedQuery(String queryName, Object[] =
values,
> Type[] types) throws DataAccessException;
> public abstract List findByNamedQuery(String queryName, Object[] =
values)
> throws DataAccessException;
> public abstract List findByNamedQuery(String queryName, String pa=
ramName,
> Object value, Type type) throws DataAccessExcepti=
on;
> public abstract List findByNamedQuery(String queryName, String pa=
ramName,
> Object value) throws DataAccessException;
> public abstract List findByNamedQuery(String queryName,
> String[] paramNames, Object[] values, Type[] type=
s)
> throws DataAccessException;
> public abstract List findByNamedQuery(String queryName,
> String[] paramNames, Object[] values) throws Data=
AccessException;
> public abstract List findByNamedQuery(String queryName)
> throws DataAccessException;
> public abstract List findByNamedQueryAndValueBean(String queryNam=
e,
> Object valueBean) throws DataAccessException;
> public abstract List findByValueBean(String queryString, Object v=
alueBean)
> throws DataAccessException;
> public abstract Object get(Serializable id, LockMode lockMode)
> throws DataAccessException;
> public abstract Object get(Serializable id) throws DataAccessExce=
ption;
> public abstract Object load(Serializable id, LockMode lockMode)
> throws DataAccessException;
> public abstract Object load(Serializable id) throws DataAccessExc=
eption;
> public abstract List loadAll() throws DataAccessException;
> public abstract void lock(Object obj,LockMode lockMode) throws Da=
taAccessException;
> public abstract void save(Object obj,Serializable id) throws Data=
AccessException;
> public abstract Serializable save(Object obj) throws DataAccessEx=
ception;
> public abstract void saveOrUpdate(Object obj) throws DataAccessEx=
ception;
> public abstract Object saveOrUpdateCopy(Object obj) throws DataAc=
cessException;
> public abstract void update(Object obj,LockMode lockMode) throws =
DataAccessException;
> public abstract void update(Object obj) throws DataAccessExceptio=
n;
> public abstract Class getClazz();
> public abstract void setClazz(Class clazz);
> }
>=20
>=20
> -------------------------------------------------------
> This SF.Net email is sponsored by Sleepycat Software
> Learn developer strategies Cisco, Motorola, Ericsson & Lucent use to
> deliver higher performing products faster, at low TCO.
> http://www.sleepycat.com/telcomwpreg.php?From=3Dosdnemail3
> _______________________________________________
> Springframework-developer mailing list
> Spr...@li...
> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>=20
>=20
>=20
>=20
> -------------------------------------------------------
> This SF.Net email is sponsored by: SourceForge.net Broadband
> Sign-up now for SourceForge Broadband and get the fastest
> 6.0/768 connection for only $19.95/mo for the first 3 months!
> http://ads.osdn.com/?ad_id%62&alloc_ida84&op=CCk
> _______________________________________________
> Springframework-developer mailing list
> Spr...@li...
> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>=20
|