Como utilizar o EntityManager?

1 resposta
diegogeovani

Saudações.

Quero utilizar o EntityManager em meu projeto. Porém, estou me deparando com este problema:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.persistence.EntityManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

Com base no que procurei na internet, o EntityManager precisa ser criado, e para isso é preciso uma certa "configuração". Como faço?

DAO:
@Component
public class SetorDao {
	
	private Query findAll;	
	private EntityManager em;
	private static Transaction transaction;
	private final Session session;		
	
	public SetorDao(Session session, EntityManager em) {		
		this.session = session;
		this.em = em;
		this.findAll = this.em.createNamedQuery("setor.findAll");	
	}
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">   
  
<hibernate-configuration>
	<session-factory>	

		<property name="hibernate.connection.username">x</property> 
		<property name="hibernate.connection.password">y</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost/industrial</property>
		
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
		
		<property name="hibernate.hbm2ddl.auto">update</property>

	<mapping class="br.com.industrial.modelo.Rms" />
	<mapping class="br.com.industrial.modelo.Usuario" />
	<mapping class="br.com.industrial.modelo.Rnc" />
	<mapping class="br.com.industrial.modelo.setor.Setor" />
	</session-factory>	
</hibernate-configuration>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>industrial</display-name>	
	<!-- configura o controlador do VRaptor -->
	<filter>
		<filter-name>vraptor</filter-name>
		<filter-class>br.com.caelum.vraptor.VRaptor</filter-class>
	</filter>
		
	<filter-mapping>
		<filter-name>vraptor</filter-name>
		<url-pattern>/*</url-pattern>
		<dispatcher>FORWARD</dispatcher>
		<dispatcher>REQUEST</dispatcher>
	</filter-mapping>
	
	<context-param>
		<param-name>br.com.caelum.vraptor.packages</param-name>
		<param-value>br.com.caelum.vraptor.util.jpa</param-value>
	</context-param>

<jsp-config>   
    <jsp-property-group>   
        <display-name>todos os jsps</display-name>   
        <url-pattern>*.jsp</url-pattern>   
        <page-encoding>ISO-8859-1</page-encoding>   
        <include-prelude>header.jspf</include-prelude>   
    </jsp-property-group>   
</jsp-config>
</web-app>
Então pessoal, quais detalhes estão faltando?

Obrigado!

1 Resposta

romarcio

Um exemplo de como configurar um sistema como JPA: http://wp.me/p1iE6J-39

Criado 6 de julho de 2012
Ultima resposta 6 de jul. de 2012
Respostas 1
Participantes 2