Pessoal,
Comecei a usar o hibernate tools no eclipse.
Todos os arquivos estão em src/bd/.
Criei um projeto Desktop, uma tabela no MySQL e o hibernate tools gerou os seguintes códigos:
Pessoa.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 04/01/2009 16:22:11 by Hibernate Tools 3.2.2.GA -->
<hibernate-mapping>
<class name="Pessoa" table="pessoa" catalog="teste">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="nome" type="string">
<column name="nome" length="45" not-null="true" />
</property>
<property name="celular" type="string">
<column name="celular" length="45" />
</property>
<property name="cpf" type="string">
<column name="cpf" length="45" not-null="true" />
</property>
<property name="end" type="string">
<column name="end" length="45" />
</property>
</class>
</hibernate-mapping>
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.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/teste</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
Pessoa.java
// default package
// Generated 04/01/2009 16:22:09 by Hibernate Tools 3.2.2.GA
package bd;
/**
* Pessoa generated by hbm2java
*/
public class Pessoa implements java.io.Serializable {
private Integer id;
private String nome;
private String celular;
private String cpf;
private String end;
public Pessoa() {
}
public Pessoa(String nome, String cpf) {
this.nome = nome;
this.cpf = cpf;
}
public Pessoa(String nome, String celular, String cpf, String end) {
this.nome = nome;
this.celular = celular;
this.cpf = cpf;
this.end = end;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNome() {
return this.nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getCelular() {
return this.celular;
}
public void setCelular(String celular) {
this.celular = celular;
}
public String getCpf() {
return this.cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getEnd() {
return this.end;
}
public void setEnd(String end) {
this.end = end;
}
}
E o DAO: PessoaHome.java
// default package
// Generated 04/01/2009 16:22:12 by Hibernate Tools 3.2.2.GA
package bd;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Example;
/**
* Home object for domain model class Pessoa.
* @see .Pessoa
* @author Hibernate Tools
*/
public class PessoaHome {
private static final Log log = LogFactory.getLog(PessoaHome.class);
private final SessionFactory sessionFactory = getSessionFactory();
protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext()
.lookup("SessionFactory");
} catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException(
"Could not locate SessionFactory in JNDI");
}
}
public void persist(Pessoa transientInstance) {
log.debug("persisting Pessoa instance");
try {
sessionFactory.getCurrentSession().persist(transientInstance);
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}
public void attachDirty(Pessoa instance) {
log.debug("attaching dirty Pessoa instance");
try {
sessionFactory.getCurrentSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Pessoa instance) {
log.debug("attaching clean Pessoa instance");
try {
sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void delete(Pessoa persistentInstance) {
log.debug("deleting Pessoa instance");
try {
sessionFactory.getCurrentSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Pessoa merge(Pessoa detachedInstance) {
log.debug("merging Pessoa instance");
try {
Pessoa result = (Pessoa) sessionFactory.getCurrentSession().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public Pessoa findById(java.lang.Integer id) {
log.debug("getting Pessoa instance with id: " + id);
try {
Pessoa instance = (Pessoa) sessionFactory.getCurrentSession().get(
"Pessoa", id);
if (instance == null) {
log.debug("get successful, no instance found");
} else {
log.debug("get successful, instance found");
}
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Pessoa instance) {
log.debug("finding Pessoa instance by example");
try {
List results = sessionFactory.getCurrentSession().createCriteria(
"Pessoa").add(Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
}
E por fim a classe que chama o método findById:
package bd;
public class Teste {
public static void main(String[] args)
{
PessoaHome gerente = new PessoaHome();
Pessoa p1 = gerente.findById(1);
}
}
Quando eu executo a classe Teste, a seguinte exceção é gerada:
04/01/2009 19:45:43 bd.PessoaHome getSessionFactory
SEVERE: Could not locate SessionFactory in JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at bd.PessoaHome.getSessionFactory(PessoaHome.java:27)
at bd.PessoaHome.<init>(PessoaHome.java:22)
at bd.Teste.main(Teste.java:11)
Exception in thread "main" java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
at bd.PessoaHome.getSessionFactory(PessoaHome.java:30)
at bd.PessoaHome.<init>(PessoaHome.java:22)
at bd.Teste.main(Teste.java:11)
Obrigado
Victor