Boa tarde Pessoal… La vem eu denovo…
Gostaria que voce me desse um toque para ver se estou indo no caminho correto a respeito da implantacao do controle de transacao do Spring. Ja li as referencias do site do Spring e nao entendi muito bem devido meu ingles ser intermediario, mas la vai. Quero deixar meu controle de transacao ser de modo declarativo. Estou usando o Hibernate e DataSource gerenciado pelo Tomcat. Duvidas:
1 - Eu devo criar um proxy para usar a demarcacao de transacao declarativa?
2- O que significa callback?
3- Pra que serve esse proxy e o Interceptor?
Vou te mostrar o meu codigo com meus arquivos e gostaria que voces me falassem se esta ok.
Minha classe e interface DAO:
public abstract class GenericHibernateDAO<T, ID extends Serializable> extends HibernateDaoSupport implements GenericDAO<T, ID> {
private Class<T> persistentClass;
public GenericHibernateDAO() {
this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public Class<T> getPersistentClass() {
return this.persistentClass;
}
public void salvar(T entity) {
this.getHibernateTemplate().save(entity);
}
public void apagar(T entity) {
this.getHibernateTemplate().delete(entity);
}
public void update(T entity) {
this.getHibernateTemplate().update(entity);
}
public T consultarPorId(ID id) {
T persistentClass = null;
persistentClass = (T) this.getHibernateTemplate().get(this.getPersistentClass(), id);
return persistentClass;
}
}
public class ClienteDAOHibernate extends GenericHibernateDAO<Cliente, Long> implements ClienteDAO {
public Collection<Contrato> retornaContratos(Cliente cliente) {
return this.getHibernateTemplate().find("from br.com.MGFSuporte.DAO.Contrato as con " +
"left join fetch con.cliente as cli Where cli.cnpj like ?", cliente.getCnpj());
}
public List<Cliente> listar() {
return this.getHibernateTemplate().find("from br.com.MGFSuporte.DAO.Cliente as cli order by cli.razaoSocial asc");
}
public Collection<Modulo> retornarModulo(Cliente cliente) {
List<Modulo> listaModulo = null;
try {
Session session = this.getSession(false);
Query query = session.createQuery("select m from br.com.MGFSuporte.DAO.ModuloContratado as mc " +
"inner join mc.modulo as m " +
"inner join mc.contrato as contr " +
"inner join contr.cliente as cli Where cli.cnpj like ? order by m.descricaoModulo asc");
query.setString(0, cliente.getCnpj());
listaModulo = query.list();
return listaModulo;
} catch (HibernateException e) {
throw e;
}
}
public Collection<Aditivo> retornaAditivo(Cliente cliente) {
List<Aditivo> listaAditivo = null;
try {
Session session = this.getSession(false);
Query query = session.createQuery("select aditivo from br.com.MGFSuporte.DAO.Aditivo as aditivo " +
"inner join aditivo.contrato as contr " +
"inner join contr.cliente as cli " +
"where cli.cnpj like ?");
query.setString(0, cliente.getCnpj());
listaAditivo = query.list();
return listaAditivo;
} catch (HibernateException e) {
throw e;
}
}
public Cliente consultaCliente(String nome) throws DataAccessException, ClienteNotFoundException {
Cliente cliente = null;
try {
Session session = this.getSession(false);
Query query = session.createQuery("from br.com.MGFSuporte.DAO.Cliente as C Where C.razaoSocial like ?");
query.setString(0, nome);
cliente = (Cliente) query.uniqueResult();
if (cliente == null) {
throw new ClienteNotFoundException("Nenhum Cliente Encontrado");
}
} catch (HibernateException he) {
this.convertHibernateAccessException(he);
}
return cliente;
}
public boolean verificaCNPJ(String cnpj) {
Session session = this.getSession(false);
Query query = session.createQuery("from br.com.MGFSuporte.DAO.Cliente as C Where C.cnpj like ?");
query.setString(0, cnpj);
if (query.uniqueResult() != null) {
return true;
} else {
return false;
}
}
}
E agora o meu arquivo de configuracao do Spring, nao sei se devo usar esse proxy que esta no codigo…
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>br/com/MGFSuporte/DAO/Cliente.hbm.xml</value>
<value>br/com/MGFSuporte/DAO/Usuario.hbm.xml</value>
<value>br/com/MGFSuporte/DAO/Contrato.hbm.xml</value>
<value>br/com/MGFSuporte/DAO/Aditivo.hbm.xml</value>
<value>br/com/MGFSuporte/DAO/Modulo.hbm.xml</value>
<value>br/com/MGFSuporte/DAO/ModuloContratado.hbm.xml</value>
<value>br/com/MGFSuporte/DAO/Autorizacao.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="show_sql">
true
</prop>
<prop key="hibernate.generic statistics">
true
</prop>
<prop key="hibernate.use_sql_comments">
true
</prop>
</props>
</property>
</bean>
<!-- Este datasource é gerenciado pelo servidor -->
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/MGFSuporte" />
</bean>
<bean id="daoAlvo"
class="br.com.MGFSuporte.DAO.GenericHibernateDAO">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="daoGenericoTransacional"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="daoAlvo"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="detete*">
PROPAGATION_REQUIRED, ISOLATION_READ_COMMITED
</prop>
<prop key="save*">
PROPAGATION_REQUIRED, ISOLATION_READ_COMMITED
</prop>
<prop key="update*">
PROPAGATION_REQUIRED, ISOLATION_READ_COMMITED
</prop>
<prop key="update*">
PROPAGATION_REQUIRED, ISOLATION_READ_COMMITED
</prop>
<prop key="get*">
PROPAGATION_SUPPORTS, readOnly
</prop>
<prop key="list*">
PROPAGATION_SUPPORTS, readOnly
</prop>
</props>
</property>
</bean>
<!-- Objeto que nos queremos que seja transacional -->
<bean id="clienteService" class="br.com.MGFSuporte.DAO.ClienteDAOHibernate"/>
<!-- O advice transacional -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- A semantina transacional -->
<tx:attributes>
<tx:method name="listar*" read-only="true"/>
<tx:method name="retorna*" read-only="true"/>
<tx:method name="consulta*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- AOP que caracteriza as execucoes -->
<aop:config>
<!-- Executa todos os joinpoints definido pelo pointcut -->
<aop:pointcut id="serviceOperation" expression="execution(*br.com.MGFSuporte.DAO.*.*(..))"/>
<!-- O advice que intercept o pointcut -->
<aop:advisor advice-ref="" pointcut-ref="serviceOperation"/>
</aop:config>
</beans>
E como eu faco para usar isso no meu controller do Struts? Devo chamar dessa forma?
ApplicationContext ap = new ClassPathXmlApplicationContext("applicationContext.xml");
ClienteDAO clienteDAO = (ClienteDAO) ap.getBean("clienteService");
clienteDAO.salvar(new Cliente());
Obrigado pela ajuda…