Obrigado pela ajuda…
Na vdd eu preciso de mais alguma ajuda:
Seguindo este link eu consegui montar um Dao generico da forma como é mostrado no tutorial.
Ficou assim…
package genericdao;
import java.io.Serializable;
public interface GenericDao<T, PK extends Serializable> {
/** Persist the newInstance object into database */
PK create(T newInstance);
/**
* Retrieve an object that was previously persisted to the database using
* the indicated id as primary key
*/
T read(PK id);
/** Save changes made to a persistent object. */
void update(T transientObject);
/** Remove an object from persistent storage in the database */
void delete(T persistentObject);
}
package genericdao.impl;
import genericdao.GenericDao;
import java.io.Serializable;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public abstract class GenericDaoHibernateImpl<T, PK extends Serializable> implements
GenericDao<T, PK> {
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return this.sessionFactory;
}
private Class<T> type;
public GenericDaoHibernateImpl() {
// TODO Auto-generated constructor stub
}
public GenericDaoHibernateImpl(Class<T> type) {
this.type = type;
}
@Transactional
public PK create(T o) {
return (PK) sessionFactory.getCurrentSession().save(o);
}
@Transactional(readOnly = true)
public T read(PK id) {
return (T) sessionFactory.getCurrentSession().get(type, id);
}
@Transactional
public void update(T o) {
sessionFactory.getCurrentSession().update(o);
}
@Transactional
public void delete(T o) {
sessionFactory.getCurrentSession().delete(o);
}
}
Ai arquivo do Spring ficou assim:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<context:component-scan base-package="genericdao.impl" />
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="courseDao" class="genericdao.impl.GenericDaoHibernateImpl">
<constructor-arg>
<value>dominio.Course</value>
</constructor-arg>
</bean>
</beans>
Pra usar eu faço assim:
package genericdao.teste;
import genericdao.GenericDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import dominio.Course;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"beans-hibernate.xml");
GenericDao dao = (GenericDao) context.getBean("courseDao");
Course course = new Course();
course.setTitle("Core DAO");
dao.create(course);
}
}
O problema com esta solução é que eu só tenho os metodos do Dao generico na interface.
Depois eu criei uma interface para incluir metodos especificos para uma determinada classe:
package genericdao;
import dominio.Course;
public interface CourseDao extends GenericDao<Course, Long> {
Course findByTitle(String courseTitle);
}
e fiz a implementação dessa interface:
package genericdao.impl;
import org.hibernate.Query;
import genericdao.CourseDao;
import dominio.Course;
public class CoursHibernateDao extends GenericDaoHibernateImpl<Course, Long>
implements CourseDao {
@Override
public Course findByTitle(String courseTitle) {
Query q = super.getSessionFactory().getCurrentSession()
.getNamedQuery("findByTitle");
q.setParameter("title", courseTitle);
return (Course) q.uniqueResult();
}
}
Só que agora eu não sei como incluir isso no arquivo do Spring pra poder usar.
Antes eu fazia assim:
<bean id="courseDao" class="genericdao.impl.GenericDaoHibernateImpl">
<constructor-arg>
<value>dominio.Course</value>
</constructor-arg>
</bean>
Será que basta trocar o GenericDaoHibernateImpl por CourseHibernateDao? Eu acho que não preciso incluir o tipo da classe como faço no construtor acima, ou preciso? Tenho que mudar alguma coisa? Será que alguém pode me dar umas dicas?
Obrigado!
Att,
Filipe Santana.