Problema ao utilizar Spring, JSF e Hibernate

2 respostas
smorigo

Bom dia pessoal, estou começando meu primeiro projeto com Spring, JSF e Hibernate e estou esbarrando em um problema bem basico.

Esse é o meu Bean:

package br.com.fci.bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity (name = "Cliente")
@Table(name = "TB_CLIENTE")
public class Cliente implements Serializable {

	@Id
    @Column(name = "CD_CLIENTE", nullable = false)
    @SequenceGenerator(name = "CODIGO_GERADO", sequenceName = "GE_CLIENTE", allocationSize = 1)    
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="CODIGO_GERADO")
	private Long codigoCliente;
	
    @Column(name = "DC_NOME_COMPLETO")	
	private String nomeCompleto;

    // implementei os get, set, hashCode e equals
}

Essa é minha interface:

package br.com.fci.dao;

import java.util.List;

import javax.faces.model.DataModel;

import br.com.fci.bean.Cliente;

public interface ClienteDAO {

	public List<Cliente> findAll();
	public void salvar(Cliente cliente);
	public void excluir(long codigoCliente);
	public Cliente procurar(long codigoCliente);	
}

Essa é a implementação:

package br.com.fci.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.orm.hibernate3.HibernateTemplate;

import br.com.fci.bean.Cliente;

public class ClienteDAOImpl implements ClienteDAO {
	
	private HibernateTemplate hibernateTemplate;
	private EntityManager em;

	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}

	private EntityManager getEntityManager() {
		return em;
	}

	public void excluir(long codigo) {
		Cliente cliente = procurar(codigo);
		if (cliente != null) {
			em.remove(cliente);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Cliente> findAll() {
		Query query = getEntityManager().createQuery("select c FROM Cliente c");
		return query.getResultList();
	}

	public Cliente procurar(long codigo) {
		return em.find(Cliente.class, codigo);
	}

	public void salvar(Cliente cliente) {
		if (cliente.getCodigoCliente() == null) {
			// new
			em.persist(cliente);
		} else {
			// update
			em.merge(cliente);
		}
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
}

Esse é meu applicationContext.xml

&lt?xml version="1.0" encoding="UTF-8"?&gt
<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.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    
    
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
      <property name="driverClassName" value="org.firebirdsql.jdbc.FBDriver"/>   
      <property name="url"             value="jdbc:firebirdsql:localhost/3050:C:/Projetos/DB/FARO_TESTE.FDB"/>   
      <property name="username"        value="SYSDBA"/>   
      <property name="password"        value="masterkey"/>   
   </bean>     
   
	 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
	   <property name="dataSource" ref="dataSource"/>
	   <property name="annotatedClasses">
	     <value>br.com.fci.bean.Cliente</value>
	   </property>
	   <property name="hibernateProperties">
	     <props>
	       <prop key="hibernate.dialect">org.hibernate.dialect.FirebirdDialect</prop>
	       <prop key="hibernate.show_sql">true</prop>
	       <prop key="hibernate.format_sql">true</prop>
	       <prop key="hibernate.use_sql_comments">true</prop>
	       <prop key="hibernate.generate_statistics">true</prop>
	     </props>
	   </property>
	 </bean>   
	   
	 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
	   <property name="sessionFactory" ref="sessionFactory" />
	 </bean>   
	 
	 <tx:annotation-driven transaction-manager="transactionManager"/>	 

	 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	   <property name="sessionFactory" ref="sessionFactory"/>
	 </bean>	     
    
    <bean id="clienteDao" class="br.com.fci.dao.ClienteDAOImpl">
    	<property name="hibernateTemplate" ref="hibernateTemplate"/>
  	</bean>

 </beans>

Meu faces-config.xml

&lt?xml version="1.0" encoding="ISO-8859-1" ?&gt

<!DOCTYPE faces-config PUBLIC
        "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
        "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">


<faces-config>
	
	<application>
		<locale-config>
			<default-locale>br</default-locale>
			<supported-locale>en</supported-locale>
		</locale-config>
	</application>	
	
	<application>
		<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
	</application>
	
	
	<managed-bean>
		<managed-bean-name>Cliente</managed-bean-name>
		<managed-bean-class>br.com.fci.dao.ClienteDAOImpl</managed-bean-class>
		<managed-bean-scope>request</managed-bean-scope>
		<managed-property>
        	<property-name>hibernateTemplate</property-name>
            <value>#{hibernateTemplate}</value>
        </managed-property>
	</managed-bean>  	

</faces-config>

Bom pessoal depois de tanto arquivo e configuração, estou tentando utilizar o metodo findAll para montar um <h:dataTable> mas quando tento acessar o findAll ele me informa que nao foi localizado…

2 Respostas

smorigo

Problema resolvido… em breve colocarei o exemplo que desenvolvi para auxiliar o pessoal…

[]s

bland

smorigo,

Teria como vocês postar aqui o seu JSP de exemplo?
Estou querendo começar a estudar JSF para “unir” ao Spring, e não tenho nada para iniciar. Se puder postar, agradeço. Ah, outra coisa, posta aqui como você resolveu o seu problema.

Abraço.

Criado 20 de fevereiro de 2007
Ultima resposta 21 de fev. de 2007
Respostas 2
Participantes 2