Crud VRaptor3 + PostgreSQL?

9 respostas
juniorsatanas

Pessoal eu já tentei de todas as formas usar o VR3, sempre aparece um problema novo ! sempre quando uso postgresql, no hsqldb fica normal, e preciso de alguma configuração para usar o postgresql ? além do hibernate.cfg.xml + o drive ? quem tiver um crud usando postgresql por favor: [email removido]

Valeu !

9 Respostas

Paulo_Silveira

Ola Junior

O Vraptor nao tem relacao com os bancos de dados, isso é exclusivo do Hibernate. Aqui, trocando o hibernate.cfg.xml para usar o postgresql, o mydvds funciona perfeitamente, desde que voce renomeie a tabela de User (usando @Table(name=…)). Posta a stacktrace do erro pra gente

juniorsatanas

Paulo Silveira Boa Tarde !

Vou Fazer assim :

1 - baixar o mydvds novo
2 - mudar o hibernate.cfg.xml para seta o drive do postgresql
3 - add o drive no meu classpath
4 - importar para o netbeans
5 - User (usando @Table(name=Usuario)). <— modificar
6- Modificar o hibernate.cfg.xml para setar a classe Usuario
7- Rodar !

Paulo O recomendado é o Gato Macho ou o Peixe Vidro ? qual vocês usam ai na Caelum com o VR3 ?

Abraço e Obrigado pela Paciêcnia !

Lagaffe

Junior

Uso o VRaptor3 com hibernate e Postgresql e funciona normalmente, sem nenhuma outra configuração que não seja a do hibernate.cfg.xml.
Já usei a mesma aplicação com o MySql.

Aqui parte do hibernate.cfg.xml:

<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost/seudatabase</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>   
        <property name="hibernate.connection.username">seuuser</property>
        <property name="hibernate.connection.password">senha</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>

Uso tomcat ou jetty nos testes do sistema (em desenvolvimento com eclipse), funciona perfeitamente com o dois. Só demora um pouco para iniciar o Jetty se seu computador for lento (ainda não tenho sistema em produção).

juniorsatanas

Lagaffe bOA nOITE !

Tu já fez alguma coisa no VR3 ? manda para mim testar aqui ! eu to no Ubuntu + postgresql + netbeans !

[email removido]

to fazendo um sisteminha aqui em jsf + xhtml + hibernate + ostgresql, coisa besta por que nao consigo rodar o VR3 !

abraço !

Paulo_Silveira

Oi Junio

Quando voce fala “nao consigo rodar”, a gente fica sem saber como te ajudar porque nao tem uma stack trace, nem um erro bem definido. Se voce der detalhes do que ocorre que nao roda, a gente pode ajudar bastante.

juniorsatanas
package br.com.caelum.vraptor.mydvds.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;

import br.com.caelum.vraptor.ioc.Component;
import br.com.caelum.vraptor.ioc.SessionScoped;

import javax.persistence.GeneratedValue;
import javax.persistence.Table;


@Component
@SessionScoped

 @Entity
 @Table(name="Usuario")
public class User implements Serializable {

	// Hibernate validator's annnotations/rules

	@GeneratedValue
        @Id
    	private Long id;


	@Length(min = 3, max = 20)
	private String login;

	// Hibernate validator's annnotations/rules
	@NotNull
	@Length(min = 6, max = 20)
	private String password;

	// Hibernate validator's annnotations/rules
	@NotNull
	@Length(min = 3, max = 100)
	private String name;

	// user to dvd mapping,
	@OneToMany(mappedBy="owner")
	private Set&lt;DvdCopy&gt; copies;

	public Set&lt;DvdCopy&gt; getCopies() {
		if (copies == null) {
			copies = new HashSet&lt;DvdCopy&gt;();
		}
		return copies;
	}

	public void setCopies(Set&lt;DvdCopy&gt; dvds) {
		this.copies = dvds;
	}




	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

}

hibernate.cfg.xml

&lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
 &lt;hibernate-configuration&gt;
   &lt;session-factory&gt;
     &lt;!-- CONFIGURANDO O HIbernate --&gt;
     &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/property&gt;
     &lt;property name="hibernate.connection.driver_class"&gt;org.postgresql.Driver&lt;/property&gt;
     &lt;property name="hibernate.connection.url"&gt;jdbc:postgresql://localhost:5432/NUTECBD&lt;/property&gt;
     &lt;property name="connection.username"&gt;root&lt;/property&gt;                 
     &lt;property name="hibernate.connection.password"&gt;debian23&lt;/property&gt;



 		&lt;mapping class="br.com.caelum.vraptor.mydvds.model.Dvd" /&gt;
 		&lt;mapping class="br.com.caelum.vraptor.mydvds.model.DvdCopy" /&gt;
 		&lt;mapping class="br.com.caelum.vraptor.mydvds.model.Usuario" /&gt;
                &lt;mapping class="br.com.caelum.vraptor.mydvds.model.Mensalidade" /&gt;
                &lt;mapping class="br.com.caelum.vraptor.mydvds.model.Pessoa" /&gt;


     &lt;/session-factory&gt;

 &lt;/hibernate-configuration&gt;
juniorsatanas
HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hibernateTransactionInterceptor': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.Session]: : Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionCreator': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactoryCreator': Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to load class declared as &lt;mapping class="br.com.caelum.vraptor.mydvds.model.Usuario"/&gt; in the configuration:; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactoryCreator': Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to load class declared as &lt;mapping class="br.com.caelum.vraptor.mydvds.model.Usuario"/&gt; in the configuration:; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionCreator': Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactoryCreator': Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to load class declared as &lt;mapping class="br.com.caelum.vraptor.mydvds.model.Usuario"/&gt; in the configuration:; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'br.com.caelum.vraptor.util.hibernate.SessionFactoryCreator': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactoryCreator': Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to load class declared as &lt;mapping class="br.com.caelum.vraptor.mydvds.model.Usuario"/&gt; in the configuration:
	org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:591)
	org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:193)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:925)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:835)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
	java.security.AccessController.doPrivileged(Native Method)
	org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
	org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:302)
	org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:43)
	org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
	org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:308)
	org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:297)
	org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:942)
	org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:224)
	br.com.caelum.vraptor.ioc.spring.VRaptorApplicationContext.getBean(VRaptorApplicationContext.java:209)
	br.com.caelum.vraptor.ioc.spring.SpringBasedContainer.instanceFor(SpringBasedContainer.java:61)
	br.com.caelum.vraptor.util.collections.Functions$1.apply(Functions.java:32)
	br.com.caelum.vraptor.util.collections.Functions$1.apply(Functions.java:30)
	com.google.common.collect.Lists$TransformingRandomAccessList.get(Lists.java:425)
	java.util.AbstractList$Itr.next(AbstractList.java:345)
	com.google.common.collect.Iterators$7.computeNext(Iterators.java:572)
	com.google.common.collect.AbstractIterator.tryToComputeNext(AbstractIterator.java:135)
	com.google.common.collect.AbstractIterator.hasNext(AbstractIterator.java:130)
	com.google.common.collect.Lists.newArrayList(Lists.java:129)
	com.google.common.collect.Collections2$FilteredCollection.toArray(Collections2.java:229)
	br.com.caelum.vraptor.interceptor.DefaultInterceptorRegistry.interceptorsFor(DefaultInterceptorRegistry.java:50)
	br.com.caelum.vraptor.interceptor.InterceptorListPriorToExecutionExtractor.intercept(InterceptorListPriorToExecutionExtractor.java:42)
	br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:46)
	br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:59)
	br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:80)
	br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:46)
	br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:59)
	br.com.caelum.vraptor.interceptor.ResourceLookupInterceptor.intercept(ResourceLookupInterceptor.java:67)
	br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:46)
	br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:59)
	br.com.caelum.vraptor.core.DefaultRequestExecution.execute(DefaultRequestExecution.java:62)
	br.com.caelum.vraptor.VRaptor$1.insideRequest(VRaptor.java:91)
	br.com.caelum.vraptor.ioc.spring.SpringProvider.provideForRequest(SpringProvider.java:55)
	br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:88)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

root cause
juniorsatanas

Bom mudei o nome da tabela :

# @Component  
# @SessionScoped  
#   
# @Entity  
# @Table(name="Usuario")  
# public class User implements Serializable {

AGORA DA ESSE ERRO ai !

Lucas_Cavalcanti

se vc olhar a stacktrace tem:

org.hibernate.MappingException: Unable to load class declared as <mapping class="br.com.caelum.vraptor.mydvds.model.Usuario"/>

sua classe se chama User, não Usuario… só mudar no hibernate.cfg.xml

Criado 11 de fevereiro de 2010
Ultima resposta 13 de fev. de 2010
Respostas 9
Participantes 4