Como utilizar o HIBERNATE(Me ajudem)

4 respostas
W

Segue o fonte do projeto de teste feito com o hibernate:

public class Amigo {

private String nome;

private String endereco;

private String telefone;

private String celular;

private String email;

private java.util.Date nascimento;

public Amigo() {
}

public String getNome(){

return nome;

}
public void setNome(String nome){

this.nome = nome;

}
public String getEndereco(){

return endereco;

}
public void setEndereco(String endereco){

this.endereco = endereco;

}
public String getTelefone(){

return telefone;

}
public void setTelefone(String telefone){

this.telefone = telefone;

}
public String getCelular(){

return celular;

}
public void setCelular(String celular){

this.celular = celular;

}
public String getEmail(){

return email;

}
public void setEmail(String email){

this.email = email;

}
public java.util.Date getNascimento(){

return nascimento;

}
public void setNascimento(java.util.Date nascimento){

this.nascimento = nascimento;

}

}

import java.util.List;

import net.sf.hibernate.*;

import net.sf.hibernate.cfg.Configuration;

public class AmigoDAO{

private SessionFactory factory;

public AmigoDAO() throws Exception{

factory = new Configuration().addClass(Amigo.class).buildSessionFactory();

}
public void insert(Amigo amigo) throws Exception{

Session session = factory.openSession();

session.save(amigo);

session.flush();

session.close();

}
public java.util.List getList(String condicao) throws Exception{

Session session = factory.openSession();

List amigos = session.find(condicao);

session.flush();

session.close();

return amigos;

}
public Amigo retrieve(String pk) throws Exception{

Session session = factory.openSession();

Amigo amigo = (Amigo)session.load(Amigo.class, pk);

session.flush();

session.close();

return amigo;

}
public void delete(Amigo amigo) throws Exception{

Session session = factory.openSession();

session.delete(amigo);

session.flush();

session.close();

}

}

public class TesteAmigo {

public static void main(String[] args) throws Exception {

try 
 { 
   Amigo amigo = new Amigo(); 
   amigo.setNome("seu nome"); 
   amigo.setEndereco("seu endereco"); 
   amigo.setTelefone("seu fone"); 
   amigo.setCelular("seu celular"); 
   amigo.setEmail("seu mail"); 
   //amigo.setNascimento("data do tipo Date"); 
      
   AmigoDAO dao = new AmigoDAO(); 
   dao.insert(amigo); 


 } 
 catch(Exception e) 
 {    
   e.printStackTrace();//aqui vc vai saber que xabu é esse. 
 }

}
}

Amigo.hbm.xml

<?xml version=“1.0”?>

<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD//EN” “<a href="http://hibernate.sourceforge.net/hibernate-mapping.dtd">http://hibernate.sourceforge.net/hibernate-mapping.dtd</a>”>

<hibernate-mapping>

<class name=“Amigo” table=“amigo”>



<generator class=“assigned”/>



<property name=“endereco” type=“string”/>

<property name=“telefone” column=“telefone” type=“string”/>

<property name=“celular” column=“celular” type=“string”/>

<property name=“email” type=“string”/>

<property name=“nascimento” type=“date”/>

</class>

</hibernate-mapping>

hibernate.properties

hibernate.dialect = net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/test
hibernate.connection.username = root
hibernate.connection.password =

log4j.properties

log4j.rootLogger=INFO, dest1

log4j.appender.dest1=org.apache.log4j.ConsoleAppender
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d %-5p %-5c{3} %x -> m%n

#log4j.appender.dest2=org.apache.log4j.RollingFileAppender
#log4j.appender.dest2.File=bridge.log

#log4j.appender.dest2.MaxFileSize=100KB

Keep one backup file

#log4j.appender.dest2.MaxBackupIndex=3

#log4j.appender.dest2.layout=org.apache.log4j.PatternLayout
#log4j.appender.dest2.layout.ConversionPattern=%d [%t] %-5p %-5c{3}(%L) %x -> %m%n

Executo e dá a seguinte mensagem:

2004-09-15 23:15:43,656 INFO  hibernate.cfg.Environment  -> m

2004-09-15 23:15:43,656 INFO  hibernate.cfg.Environment  -> m

2004-09-15 23:15:43,671 INFO  hibernate.cfg.Environment  -> m

2004-09-15 23:15:43,671 INFO  hibernate.cfg.Configuration  -> m

2004-09-15 23:15:45,359 INFO  hibernate.cfg.Binder  -> m

2004-09-15 23:15:45,453 INFO  hibernate.cfg.Configuration  -> m

2004-09-15 23:15:45,453 INFO  hibernate.cfg.Configuration  -> m

2004-09-15 23:15:45,453 INFO  hibernate.cfg.Configuration  -> m

2004-09-15 23:15:45,484 WARN  hibernate.cfg.SettingsFactory  -> m

2004-09-15 23:15:45,484 INFO  hibernate.dialect.Dialect  -> m

2004-09-15 23:15:45,484 INFO  hibernate.cfg.SettingsFactory  -> m

2004-09-15 23:15:45,531 WARN  hibernate.connection.UserSuppliedConnectionProvider  -> m

2004-09-15 23:15:45,546 INFO  hibernate.transaction.TransactionManagerLookupFactory  -> m

2004-09-15 23:15:45,546 INFO  hibernate.cfg.SettingsFactory  -> m

2004-09-15 23:15:45,546 INFO  hibernate.cfg.SettingsFactory  -> m

2004-09-15 23:15:45,546 INFO  hibernate.cfg.SettingsFactory  -> m

2004-09-15 23:15:45,546 INFO  hibernate.cfg.SettingsFactory  -> m

2004-09-15 23:15:45,562 INFO  hibernate.cfg.SettingsFactory  -> m

2004-09-15 23:15:45,578 INFO  hibernate.cfg.Configuration  -> m

2004-09-15 23:15:45,734 INFO  hibernate.impl.SessionFactoryImpl  -> m

2004-09-15 23:15:46,203 INFO  hibernate.impl.SessionFactoryObjectFactory  -> m

java.lang.UnsupportedOperationException: The user must supply a JDBC connection

at net.sf.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:32)

at net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:286)

at net.sf.hibernate.impl.SessionImpl.connect(SessionImpl.java:3326)

at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3286)

at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:61)

at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:56)

at net.sf.hibernate.impl.BatcherImpl.prepareBatchStatement(BatcherImpl.java:109)

at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:460)

at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:442)

at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29)

at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2418)

at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2371)

at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)

at AmigoDAO.insert(AmigoDAO.java:16)

at TesteAmigo.main(TesteAmigo.java:16)

SE alguém puder me ajudar, ficarei muito agradecido:

Obs.: Fiz pesquisa no site e não achei algo parecido o erro acima é na linha flush().

4 Respostas

Rodrigo_Carvalho_Aul

Provavelmente o hibernate.properties não está no classpath…

[]'s

W

Qual a pasta em que deve ficar e como será o CLASSPATH?

O meu está assim:

C:JAVAPOS TIHIBERNATEhibernate.properties

W

Qual a pasta em que deve ficar e como será o CLASSPATH?

O meu está assim:

C:\JAVA\POS TI\HIBERNATE\hibernate.properties

W

o classpath e o caminho de suas classes

se for web vc coloca hiem:
/WEB-INF/classes

se não for coloca aonde estão as suas classes

Criado 15 de setembro de 2004
Ultima resposta 16 de set. de 2004
Respostas 4
Participantes 3