私はSpringには新しく、この記事のようにGeneric DAOを実装しようとしていますhttp://www.ibm.com/developerworks/library/j-genericdao/。私はエンティティ - ConcreteEntity1とConcreteEntity2のカップルを持っています。また、私はクラスコンストラクタでの汎用型のオートワイヤリング[Spring 4.2.5]
public interface GenericDao<T extends Serializable> {
public T get(long id);
public List<T> get(String hql);
public void remove(T persistentObject);
public void add(T entity);
}
と
@Repository("hibGenericDao")
public class HibGenericDaoImpl<T extends Serializable> implements GenericDao<T> {
@Autowired
private SessionFactory sessionFactory;
private Class<T> type;
public HibGenericDaoImpl(Class<T> type) {
this.type = type;
}
/** {@inheritDoc} */
@Override
public T get(long id) {
T entity;
try (Session session = sessionFactory.getCurrentSession()) {
entity = session.get(type, id);
}
return entity;
}
/** {@inheritDoc} */
@Override
public List<T> get(String hql) {
List<T> entityList;
try (Session session = sessionFactory.getCurrentSession()) {
Query query = session.createQuery(hql);
entityList = query.list();
}
return entityList;
}
/** {@inheritDoc} */
@Override
public void remove(T persistentObject) {
try (Session session = sessionFactory.getCurrentSession()) {
session.delete(persistentObject);
}
}
/** {@inheritDoc} */
@Override
public void add(T entity) {
try (Session session = sessionFactory.getCurrentSession()) {
session.saveOrUpdate(entity);
}
}
}
今私は、サービス層の書き込みしようとしているしている、と私はフィールドtype
がConcreteEntity1.class
が含まれているところHibGenericDaoImpl<ConcreteEntity1>
をautowireたいです。 XMLなしで実行する方法を教えてください。