Agora que estou no meu micro de casa aí vai um exemplo completo de XDoclet com Hibernate (usei o exemplodo tutorial do GUJ, pois está bem claro
) :
/*
* Created on 21/09/2003
* $Revision$
* $Source$
* $Log$
*/
package dao;
import modelo.Amigo;
import cirrus.hibernate.Datastore;
import cirrus.hibernate.Hibernate;
import cirrus.hibernate.Session;
import cirrus.hibernate.SessionFactory;
/** Template do Douglas
* @author <a href="mailto:douglasfsbr@yahoo.com.br">Douglas</a>
* @version $Id$
*/
public class AmigoDAO {
private SessionFactory factory;
public AmigoDAO() throws Exception {
Datastore datastore = Hibernate.createDatastore();
datastore.storeClass(Amigo.class);
factory = datastore.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();
java.util.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();
}
}
/*
* Created on 21/09/2003
* $Revision$
* $Source$
* $Log$
*/
package logica;
import java.util.GregorianCalendar;
import modelo.Amigo;
import dao.AmigoDAO;
/** Template do Douglas
* @author <a href="mailto:douglasfsbr@yahoo.com.br">Douglas</a>
* @version $Id$
*/
public class PersistirAmigo {
public static void main(String[] args) {
Amigo amigo = new Amigo();
amigo.setCelular("5555-7777");
amigo.setEmail("douglasfsbr@yahoo.com.br");
amigo.setEndereco("Rua da minha casa");
amigo.setNascimento(new GregorianCalendar(1982,6,23).getTime());
amigo.setNome("Douglas");
amigo.setTelefone("9999-8888");
try {
AmigoDAO amigoDAO = new AmigoDAO();
amigoDAO.insert(amigo);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/*
* Created on 20/09/2003
* $Revision$
* $Source$
* $Log$
*/
package modelo;
/** Template do Douglas
* @author <a href="mailto:douglasfsbr@yahoo.com.br">Douglas</a>
* @version $Id$
* @hibernate.class table = "amigos"
*/
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() {
}
/**
* @return
* @hibernate.property column = "cel" type = "string"
*/
public String getCelular() {
return celular;
}
/**
* @return
* @hibernate.property type = "string"
*/
public String getEmail() {
return email;
}
/**
* @return
* @hibernate.property type="string"
*/
public String getEndereco() {
return endereco;
}
/**
* @return
* @hibernate.property type="date"
*/
public java.util.Date getNascimento() {
return nascimento;
}
/**
* @return
* @hibernate.id generator-class = "assigned" column = "nome"
* type = "string"
*/
public String getNome() {
return nome;
}
/**
* @return
* @hibernate.property column = "fone" type="string"
*/
public String getTelefone() {
return telefone;
}
/**
* @param string
*/
public void setCelular(String string) {
celular = string;
}
/**
* @param string
*/
public void setEmail(String string) {
email = string;
}
/**
* @param string
*/
public void setEndereco(String string) {
endereco = string;
}
/**
* @param date
*/
public void setNascimento(java.util.Date date) {
nascimento = date;
}
/**
* @param string
*/
public void setNome(String string) {
nome = string;
}
/**
* @param string
*/
public void setTelefone(String string) {
telefone = string;
}
}
conteudo do arquivo hibernate.properties (nao esqueça de colocar a pasta que este arquivo está armazenado no classpath dentro do projeto do eclipse) :
# o driver
hibernate.connection.driver_class = org.gjt.mm.mysql.Driver
# a url
hibernate.connection.url = jdbc:mysql://localhost:3306/hibernate
# o username
hibernate.connection.username = root
# a senha
hibernate.connection.password =
No exemplo acima hibernate é o nome do meu database no MySQL
Conteúdo do arquivo .xdoclet (tipo, no JBoss IDE você configura umas coisas e ele grava nesse arquivo, voce pode copiar esse arquivo dentro do seu projeto no Eclipse que o JBoss IDE vai reconhece-lo) :
<?xml version="1.0" encoding="UTF-8"?>
<configurations>
<configuration name="hibernate" used="true">
<task name="hibernatedoclet" used="true" className="xdoclet.modules.hibernate.HibernateDocletTask">
<attribute name="excludedTags" value="@version,@author,@todo" used="true"/>
<attribute name="destDir" value="src" used="true"/>
<attribute name="mergeDir" value="" used="false"/>
<attribute name="force" value="" used="false"/>
<attribute name="verbose" value="" used="false"/>
<attribute name="addedTags" value="" used="false"/>
<attribute name="ignoredtags" value="" used="false"/>
<attribute name="validating" value="" used="false"/>
<element name="fileset" used="true">
<attribute name="dir" value="src" used="true"/>
<attribute name="includes" value="**/*.java" used="true"/>
<attribute name="excludes" value="" used="false"/>
</element>
<element name="hibernate" used="true">
<attribute name="Version" value="" used="false"/>
<attribute name="useIds" value="" used="false"/>
<attribute name="xmlencoding" value="" used="false"/>
<attribute name="schema" value="" used="false"/>
<attribute name="validateXML" value="" used="false"/>
<attribute name="acceptInterfaces" value="" used="false"/>
<attribute name="acceptAbstractClasses" value="" used="false"/>
<attribute name="packageSubstitutions" value="" used="false"/>
<attribute name="packageSubstitutionInheritanceSupported" value="" used="false"/>
<attribute name="subTaskClassName" value="" used="false"/>
<attribute name="prefixWithPackageStructure" value="" used="false"/>
<attribute name="destinationFile" value="" used="false"/>
<attribute name="templateURL" value="" used="false"/>
<attribute name="templateFile" value="" used="false"/>
<attribute name="Extent" value="" used="false"/>
<attribute name="havingClassTag" value="" used="false"/>
<attribute name="ofType" value="" used="false"/>
<attribute name="subTaskName" value="" used="false"/>
<attribute name="destDir" value="src" used="true"/>
<attribute name="mergeDir" value="" used="false"/>
<attribute name="currentMethodTag" value="" used="false"/>
<attribute name="currentClassTag" value="" used="false"/>
<attribute name="currentFieldTag" value="" used="false"/>
<attribute name="currentPackage" value="" used="false"/>
<attribute name="currentMethod" value="" used="false"/>
<attribute name="currentConstructor" value="" used="false"/>
<attribute name="currentField" value="" used="false"/>
<attribute name="currentClass" value="" used="false"/>
</element>
</task>
</configuration>
</configurations>
Script ANT que é gerado automaticamente pelo JBoss IDE (xdoclet-build.xml) :
<?xml version="1.0" encoding="UTF-8"?>
<project default="_generation_" name="XDoclet Generator">
<path id="xdoclet.classpath"><pathelement location="C:/Desenvolvimento/Eclipse/Hibernate/bin"/>
<pathelement location="C:/Sun/AppServer7/jdk/jre/lib/rt.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/xml-apis.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/c3p0.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/cglib.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/commons-collections.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/commons-dbcp.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/commons-lang.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/commons-logging.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/commons-pool.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/j2ee.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/jcs.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/jdom.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/junit.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/odmg.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/optional.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/xalan.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/xerces.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/lib/ant.jar"/>
<pathelement location="C:/Java/Banco de dados/hibernate-1.2.5/hibernate-1.2/hibernate.jar"/>
<fileset dir="C:/Eclipse/plugins/org.jboss.ide.eclipse.xdoclet.core_1.1.0/">
<include name="*.jar"/>
</fileset>
</path>
<target name="_generation_" depends="N400004"/>
<target name="N400004" description="hibernate">
<taskdef classpathref="xdoclet.classpath" classname="xdoclet.modules.hibernate.HibernateDocletTask" name="hibernatedoclet"/><hibernatedoclet excludedTags="@version,@author,@todo" destDir="src" >
<fileset dir="src" includes="**/*.java" >
</fileset>
<hibernate destDir="src" >
</hibernate>
</hibernatedoclet></target>
</project>
Só não esqueça de :
Colocar no classpath do seu projeto no Eclipse os jars do Hibernate e do driver do MySQL (se vc utilizar o MySQL, caso contrário do seu banco de dados preferido).
Eu configurei meu projeto para armazenar os fontes no diretório “src” e os .class no diretório “bin”, para fazer isso :
Window/Preferences/Java/New Project e selecione a opção folders (ele grava o fonte no src e o .class no bin) e escolha também “As JRE Library Use : JRE_LIB variable”
Agora basta executar a classe PersistirAmigo e sair para o abraço :lol:
Espero ter ajudado