Iniciante VRaptor + Hibernate 4 + JBoss

Bom dia,

Resolvi começar a estudar sobre o VRaptor, vi muita gente falando bem dele.

Então, ja pesquisei bastante na net, e aqui no forum tb, estou com 1 problema, nao to conseguindo fazer o deploy da minha aplicacao, esta dando erro, segue abaixo o erro e os arquivos:

10:59:33,475 ERROR [stderr] (MSC service thread 1-4) log4j:WARN No appenders could be found for logger (br.com.caelum.vraptor.config.BasicConfiguration).

10:59:33,475 ERROR [stderr] (MSC service thread 1-4) log4j:WARN Please initialize the log4j system properly.

10:59:33,475 ERROR [stderr] (MSC service thread 1-4) log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

10:59:33,723 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/vraptor]] (MSC service thread 1-4) Exception starting filter vraptor: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactoryCreator': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: No Persistence provider for EntityManager named default
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133) [org.springframework.beans-3.0.5.RELEASE.jar:]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:394) [org.springframework.beans-3.0.5.RELEASE.jar:]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1413) [org.springframework.beans-3.0.5.RELEASE.jar:]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) [org.springframework.beans-3.0.5.RELEASE.jar:]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [org.springframework.beans-3.0.5.RELEASE.jar:]
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) [org.springframework.beans-3.0.5.RELEASE.jar:]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [org.springframework.beans-3.0.5.RELEASE.jar:]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) [org.springframework.beans-3.0.5.RELEASE.jar:]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) [org.springframework.beans-3.0.5.RELEASE.jar:]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) [org.springframework.beans-3.0.5.RELEASE.jar:]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) [org.springframework.context-3.0.5.RELEASE.jar:]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) [org.springframework.context-3.0.5.RELEASE.jar:]
	at br.com.caelum.vraptor.ioc.spring.SpringBasedContainer.start(SpringBasedContainer.java:106) [vraptor-3.4.1.jar:]
	at br.com.caelum.vraptor.ioc.spring.SpringProvider.start(SpringProvider.java:87) [vraptor-3.4.1.jar:]
	at br.com.caelum.vraptor.VRaptor.init(VRaptor.java:108) [vraptor-3.4.1.jar:]
	at br.com.caelum.vraptor.VRaptor.init(VRaptor.java:102) [vraptor-3.4.1.jar:]
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:447) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3245) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
	at org.apache.catalina.core.StandardContext.start(StandardContext.java:3836) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
	at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:70) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1824)
	at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1759)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [:1.7.0_03]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [:1.7.0_03]
	at java.lang.Thread.run(Unknown Source) [:1.7.0_03]
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named default
	at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69) [hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final]
	at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) [hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final]
	at br.com.caelum.vraptor.util.jpa.EntityManagerFactoryCreator.create(EntityManagerFactoryCreator.java:41) [vraptor-3.4.1.jar:]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.7.0_03]
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [:1.7.0_03]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [:1.7.0_03]
	at java.lang.reflect.Method.invoke(Unknown Source) [:1.7.0_03]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:340) [org.springframework.beans-3.0.5.RELEASE.jar:]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:293) [org.springframework.beans-3.0.5.RELEASE.jar:]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:130) [org.springframework.beans-3.0.5.RELEASE.jar:]
	... 24 more

10:59:33,726 ERROR [org.apache.catalina.core.StandardContext] (MSC service thread 1-4) Error filterStart
10:59:33,726 ERROR [org.apache.catalina.core.StandardContext] (MSC service thread 1-4) Context [/vraptor] startup failed due to previous errors
10:59:33,738 INFO  [org.jboss.web] (MSC service thread 1-4) registering web context: /vraptor
10:59:33,765 INFO  [org.jboss.as.server.controller] (DeploymentScanner-threads - 2) Deployed "vraptor.war"
10:59:34,322 WARN  [org.jboss.as.protocol] (pool-2-thread-2) JBAS012100: Got error closing channel JBREM00206: Channel is not open

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="vraptor">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>br.com.k2studio.vraptor.entity.Grupo</class>
    <class>br.com.k2studio.vraptor.entity.Usuario</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/teste" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.username" value="postgres" />
            <property name="hibernate.connection.password" value="p001389" />
        </properties>
     </persistence-unit>
</persistence>

orm.xml

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.0"
     xmlns="http://java.sun.com/xml/ns/persistence/orm"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
 
     <named-query name="selectGrupos">
          <query>SELECT obj FROM Grupo obj ORDER BY obj.nmGrupo</query>
     </named-query>
 
     <named-query name="selectUsuarios">
          <query>SELECT obj FROM Usuario obj ORDER BY obj.nmUsuario</query>
     </named-query>
 
</entity-mappings>

E onde crio o entityManagerFactory

JPAFactory

public class JPAFactory {

	private static EntityManagerFactory factory;
	
	public JPAFactory() {}
	
	public static EntityManager getEntityManager() {
		if (factory == null) {
			factory = Persistence.createEntityManagerFactory("vraptor");
		}
		return factory.createEntityManager();
	}
	
}

Detalhe, os arquivos orm.xml e persistence.xml estao na pasta src/META-INF, so fiz criar nela esses arquivos.

Ja agradeço pela ajuda =D

Esqueci de falar, uso o JBoss 7.0

troque no persistence.xml:

<persistence-unit name="vraptor"> 

por

<persistence-unit name="default"> 

o componente padrão do vraptor procura com esse nome.

Opa lucas, de antemao agradeço sua ajuda.

Sobre a troca daquele nome para default, eu fiz isso mas mostra a mesma mensagem de erro. Troquei tb no meu JPAFactory e tb continua o mesmo erro.

Nao sei se ta muito certo, mas ai vai meu web.xml, pode ser que tenha errado alguma coisa sem conhecer. E outra coisa, no web.xml eu declaro como provider a classe da JPa, e no persistence, o do Hibernate, nao sei se tem algo a ver.

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>vraptor</display-name>
  
  <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.provider</param-name>  
    <param-value>br.com.caelum.vraptor.util.jpa.JPACustomProvider</param-value>  
  </context-param>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

a sua pasta src é uma source folder? META-INF tá escrito certo? persistence.xml tá escrito certo?

Vo postar a imagem do meu workspace, eu peguei um tutorial da net, e ate baixei a fonte do cara que criou o tutorial, e ate as libs, mas rola Naaaaadaaaa…heheheheh

segue o link da imagem: http://i902.photobucket.com/albums/ac222/rof20004/workspace_zps1a240156.png

vlw

up, sem bronca =D

troque o

<context-param>    
    <param-name>br.com.caelum.vraptor.provider</param-name>    
    <param-value>br.com.caelum.vraptor.util.jpa.JPACustomProvider</param-value>    
  </context-param>  

por

<context-param>    
    <param-name>br.com.caelum.vraptor.packages</param-name>    
    <param-value>br.com.caelum.vraptor.util.jpa</param-value>    
  </context-param>  

e você trocou o nome do persistence-unit?

Sim lucas troquei o nome do persistence-unit para default, vou fazer o teste dessa mudanca que voce me falou agora mesmo, um minuto ‘-’

Deu certo nao, resolvi tirar todo o context e deu o deploy certinho, que estranho, mas agora aparece outro erro, quando vou carregar a pagina, vo postar aqui:

package br.com.k2studio.vraptor.dao;
 
import java.util.List;

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

import br.com.caelum.vraptor.ioc.Component;
import br.com.k2studio.vraptor.jpa.BaseEntity;
import br.com.k2studio.vraptor.jpa.JPAFactory;
 
@Component
public class GenericDAO {
 
     protected EntityManager manager;
 
     public GenericDAO() {
          manager = JPAFactory.getEntityManager();
     }
 
     public BaseEntity insert(BaseEntity entity) {
          manager.getTransaction().begin();
          manager.persist(entity);
          manager.getTransaction().commit();
          return entity;
     }
 
     public BaseEntity update(BaseEntity entity) {
          manager.getTransaction().begin();
          manager.merge(entity);
          manager.getTransaction().commit();
          return entity;
     }
 
     public void remove(BaseEntity entity) {
          entity = selectById(entity);
          manager.getTransaction().begin();
          manager.remove(entity);
          manager.getTransaction().commit();
     }
 
     public BaseEntity selectById(BaseEntity entity) {
          entity = manager.find(entity.getClass(), entity.getId());
 
          return entity;
     }
 
     public List<BaseEntity> selectByNamedQuery(String namedQuery) {
          return selectByNamedQuery(namedQuery, new Object[0]);
     }
 
     public List<BaseEntity> selectByNamedQuery(String namedQuery, Object ... parameters) {
          try {
               Query query = manager.createNamedQuery(namedQuery);
               if(parameters != null && parameters.length > 0) {
                    for (int i = 0; i < parameters.length; i++) {
                         query.setParameter(i + 1, parameters[i]);
                    }
               }
               List list = query.getResultList();
               return list;
          } catch (Exception ex) {
               ex.printStackTrace();
          }
          return null;
     }
 
}

Errei, é esse o erro:

deve ter alguma outra coisa na stacktrace… algum caused-by que explique o problema…

o que vc quis dizer com tirar todo o context?

Lucasssss

Uhu mano, consegui!!!

O problema é que faltava um construtor VAZIO no GenericDAO, agora o porque eu nao sei, tive que fazer uma gambiarra mesmo, adicionando 2 construtores, 1 vazio e o outro com uma assinatura que nao é utilizada, da uma olhada ae:

package br.com.k2studio.vraptor.dao;
 
import java.util.List;

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

import br.com.caelum.vraptor.ioc.Component;
import br.com.k2studio.vraptor.jpa.BaseEntity;
import br.com.k2studio.vraptor.jpa.JPAFactory;

@Component
public class GenericDAO {
 
     private EntityManager manager;
 
     public GenericDAO() {}
     
     public GenericDAO(EntityManager manager) {
          this.manager = JPAFactory.getEntityManager();
     }


  // Resto do codigo

Ahhh, obrigado pela ajuda, eu realmente nao sabia que o VRaptor usava default no persistence-unit.