Ok … Aqui está o mapping do hibernate
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="br.com.toyota.dss.hibernate.SvdCartaIcm"
table="SVD_CARTA_ICMS"
schema="PUB"
>
<id
name="pk"
type="java.lang.String"
column="CGC_CPF"
length="36"
>
<generator class="assigned" />
</id>
<property
name="nome"
type="java.lang.String"
column="NOME"
length="80"
/>
<property
name="endereco"
type="java.lang.String"
column="ENDERECO"
length="80"
/>
<property
name="numero"
type="java.lang.String"
column="NUMERO"
length="12"
/>
<property
name="complemento"
type="java.lang.String"
column="COMPLEMENTO"
length="20"
/>
<property
name="bairro"
type="java.lang.String"
column="BAIRRO"
length="50"
/>
<property
name="cidade"
type="java.lang.String"
column="CIDADE"
length="50"
/>
<property
name="estado"
type="java.lang.String"
column="ESTADO"
length="4"
/>
<property
name="codPais"
type="int"
column="COD_PAIS"
length="10"
/>
<property
name="cep"
type="java.lang.String"
column="CEP"
length="18"
/>
<property
name="telefone"
type="java.lang.String"
column="TELEFONE"
length="30"
/>
<property
name="fax"
type="java.lang.String"
column="FAX"
length="30"
/>
<property
name="email"
type="java.lang.String"
column="EMAIL"
length="100"
/>
<property
name="rg"
type="java.lang.String"
column="RG"
length="28"
/>
<property
name="inscEst"
type="java.lang.String"
column="INSC_EST"
length="30"
/>
<property
name="inscMunic"
type="java.lang.String"
column="INSC_MUNIC"
length="30"
/>
<property
name="inscSuframa"
type="java.lang.String"
column="INSC_SUFRAMA"
length="30"
/>
<property
name="contato"
type="java.lang.String"
column="CONTATO"
length="40"
/>
<property
name="situacao"
type="java.lang.String"
column="SITUACAO"
length="2"
/>
<property
name="sexo"
type="java.lang.String"
column="SEXO"
length="2"
/>
<property
name="dtSolicita"
type="java.sql.Date"
column="DT_SOLICITA"
length="10"
/>
<property
name="solicitante"
type="java.lang.String"
column="SOLICITANTE"
length="30"
/>
<property
name="codFornec"
type="int"
column="COD_FORNEC"
length="10"
/>
<property
name="hrSolicita"
type="int"
column="HR_SOLICITA"
length="10"
/>
<property
name="divisao"
type="java.lang.String"
column="DIVISAO"
length="4"
/>
<property
name="modelo"
type="java.lang.String"
column="MODELO"
length="32"
/>
<property
name="dtEmissao"
type="java.sql.Date"
column="DT_EMISSAO"
length="10"
/>
<property
name="hrEmissao"
type="int"
column="HR_EMISSAO"
length="10"
/>
<property
name="userEmissao"
type="java.lang.String"
column="USER_EMISSAO"
length="30"
/>
<!-- associations -->
</class>
</hibernate-mapping>
Aqui está o velho conhecido mbean do hibernate
<server>
<classpath codebase="." archives="ejb_dss.jar"/>
<mbean code="net.sf.hibernate.jmx.HibernateService" name="jboss.jca:service=HibernateFactory,
name=HibernateFactory">
<depends>jboss.jca:service=RARDeployer</depends>
<depends>jboss.jca:service=LocalTxCM,name=ProgressDS</depends>
<!-- Make it deploy ONLY after DataSource had been started -->
<attribute name="MapResources">mapping/SvdCartaIcm.hbm.xml</attribute>
<attribute name="JndiName">java:/hibernate/HibernateFactory</attribute>
<attribute name="Datasource">java:/ProgressDS</attribute>
<attribute name="Dialect">net.sf.hibernate.dialect.ProgressDialect</attribute>
<attribute name="TransactionStrategy">net.sf.hibernate.transaction.JTATransactionFactory</attribute>
<attribute name="TransactionManagerLookupStrategy">net.sf.hibernate.transaction.JBossTransactionManagerLookup</attribute>
<attribute name="UseOuterJoin">false</attribute>
<attribute name="ShowSql">false</attribute>
<attribute name="UserTransactionName">java:/actuate</attribute>
</mbean>
</server>
Aqui está o código de persistência jboss - hibernate
package br.com.toyota.dss.sessions;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.List;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import br.com.toyota.dss.interfaces.ActionCode;
/**
*
* @author Rodrigo di Lorenzo Lopes
*
* @ejb.bean description = "Sessao para peristencia dos pojos"
* display-name = "SessionBean para peristencia dos pojos"
* jndi-name = "ejb/production/PersistencySession"
* local-jndi-name = "ejb/production/PersistencySession"
* type = "Stateless"
* generate = "true"
* transaction-type = "Container"
* view-type = "both"
*
* @ejb.transaction type = "Required"
* @ejb.util generate = "physical"
*
*/
public class PersistencySessionBean implements SessionBean {
public SessionContext sessionContext;
private static SessionFactory factory;
public void ejbCreate() {}
public void ejbActivate() throws EJBException, RemoteException {}
public void ejbPassivate() throws EJBException, RemoteException {}
public void ejbRemove() throws EJBException, RemoteException {}
public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {
sessionContext = arg0;
}
/**
* Abre uma nova sessao com lookup do contexto inicial
* @return uma nova sessão aberta
* @throws NamingException a excessao lancada pela definicao
* errada no jndi
* @throws HibernateException a excessao do hibernate em abrir
* uma sessao
*/
private Session openSession() throws HibernateException, NamingException {
if (factory== null){
Context context = new InitialContext();
factory = (SessionFactory) context.lookup("java:/hibernate/HibernateFactory");
}
return factory.openSession();
}
public void setSessionFactory (SessionFactory factory){
PersistencySessionBean.factory = factory;
}
public void action (int code, Object pojo) {
Session session = null;
try {
session = this.openSession();
switch (code) {
case ActionCode.Add:
session.save(pojo);
break;
case ActionCode.delete:
session.delete(pojo);
break;
case ActionCode.update:
session.update(pojo);
break;
}
session.flush();
if (ActionCode.delete != code) session.refresh(pojo);
} catch (HibernateException e) {
if (!session.isConnected()){
System.out.println("Session is not Connected!");
}
if (!session.isOpen()){
System.out.println("Session is not open");
}
System.out.println("HibernatException");
System.out.println("Pojo = " + pojo);
switch (code) {
case ActionCode.Add:
System.err.println("Problem with adding");
break;
case ActionCode.delete:
System.out.println("Problem with deletting");
break;
case ActionCode.update:
System.out.println("Problem with updating");
break;
}
e.printStackTrace();
} catch (NamingException e) {
System.err.println("NamingException");
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
session.close();
} catch (HibernateException e1) {
System.err.println("I cannot close this session");
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
/**
*
*@ejb.interface-method view-type = "both"
*/
public void add(Object pojo) throws HibernateException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, NamingException {
System.out.println("code=" + ActionCode.Add);
action (ActionCode.Add, pojo);
}
/**
*
*@ejb.interface-method view-type = "both"
*/
public void delete(Object Pojo) throws HibernateException, NamingException {
System.out.println("code=" + ActionCode.delete);
action(ActionCode.delete, Pojo);
}
/**
*
*@ejb.interface-method view-type = "remote"
*/
public void update(Object Pojo) throws HibernateException, NamingException {
System.out.println("code=" + ActionCode.update);
action(ActionCode.update, Pojo);
}
/**
*
*@ejb.interface-method view-type = "remote"
*/
public Object findbypk(Class classPojo, Serializable pk) throws HibernateException{
Session session = null;
try {
session = this.openSession();
Object pojo = (Object) session.load(classPojo, pk);
return pojo;
} catch (HibernateException e) {
System.err.println("hibernate Exception: Erro ao Carregar pojo");
System.err.println("name of classPojo=" + classPojo);
throw e;
} catch (NamingException e) {
e.printStackTrace();
}
finally{
session.close();
}
return null;
}
/**
*
*@ejb.interface-method view-type = "remote"
*/
public List find(String className, String field, String value) throws HibernateException {
Session session = null;
List list = null;
try {
session = this.openSession();
String query ="FROM " + className + " as table WHERE table." + field + "= '" + value+ "'";
System.out.println(query + "," + value+ "Hibernate.STRING");
list = session.find(query);
} catch (HibernateException e) {
System.err.println("hibernate Exception: Query Exception!");
throw e;
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
session.close();
}
return list;
}
}
e finalmente … um test de unidade …
/*
* Created on 21/10/2003
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package br.com.toyota.dss.sessions;
import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import javax.naming.NamingException;
import junit.framework.TestCase;
import net.sf.hibernate.HibernateException;
import br.com.toyota.dss.hibernate.SvdCartaIcm;
import br.com.toyota.dss.interfaces.PersistencySession;
import br.com.toyota.dss.interfaces.PersistencySessionHome;
import br.com.toyota.dss.utils.PersistencySessionUtil;
/**
* @author Rodrigo di Lorenzo Lopes
*
*/
public class SimpleTest extends TestCase {
private PersistencySession session = null;
private PersistencySessionHome home;
private SvdCartaIcm cartaIcms;
/**
* Constructor for SvdCartaIcmTest.
* @param arg0
*/
public SimpleTest(String arg0) {
super(arg0);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(SimpleTest.class);
}
public static SvdCartaIcm aSvdCartaIcm (){
SvdCartaIcm cartaIcms = new SvdCartaIcm();
cartaIcms.setBairro("Butantã");
cartaIcms.setCep("05360-150");
cartaIcms.setCidade("São Paulo");
cartaIcms.setCodFornec(0);
cartaIcms.setCodPais(55);
cartaIcms.setComplemento("apt 61ag");
cartaIcms.setContato("Rodrigo");
cartaIcms.setDivisao("Hillux");
Calendar calendar = Calendar.getInstance();
calendar.set(2003, 9, 23);
cartaIcms.setDtEmissao(calendar.getTime());
calendar.set(2003, 11, 11);
cartaIcms.setDtSolicita(calendar.getTime());
cartaIcms.setEmail("[email removido]");
cartaIcms.setEndereco("Av Sao Remo");
cartaIcms.setEstado("SP");
cartaIcms.setFax("37356368");
cartaIcms.setHrEmissao(12);
cartaIcms.setHrSolicita(17);
cartaIcms.setInscEst("");
cartaIcms.setInscMunic("");
cartaIcms.setInscSuframa("");
cartaIcms.setModelo("ThunderBold");
cartaIcms.setNome("Rodrigo di Lorenzo Lopes");
cartaIcms.setNumero("463");
cartaIcms.setPk("[telefone removido]");
cartaIcms.setRg("M9161846");
cartaIcms.setSexo("Masculino");
cartaIcms.setSituacao("Pendente");
cartaIcms.setSolicitante("Rodrigo");
cartaIcms.setTelefone("246546368");
cartaIcms.setUserEmissao("rlopes");
return cartaIcms;
}
protected void setUp() throws Exception {
super.setUp();
if (session == null){
cartaIcms = SimpleTest.aSvdCartaIcm();
home = PersistencySessionUtil.getHome();
System.out.println("Adicionando nova Carta de icms no setUp");
session = home.create();
session.add(cartaIcms);
}
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
session.delete(cartaIcms);
session.remove();
}
public void testAdd() throws HibernateException, RemoteException {
SvdCartaIcm cartaIcms2 = null;
// cartaIcms2 = (SvdCartaIcm) session.findbypk(SvdCartaIcm.class, cartaIcms.getPk());
List list = session.find("SvdCartaIcm", "pk", cartaIcms.getPk());
Iterator iter = list.iterator();
if (!list.isEmpty()){
cartaIcms2 = (SvdCartaIcm)iter.next();
System.out.println("nome=>"+cartaIcms2.getNome());
}
else {
System.err.println("Lista vazia");
}
assertEquals("Rodrigo di Lorenzo Lopes", cartaIcms2.getNome());
}
public void testFindByPk () throws HibernateException, RemoteException{
SvdCartaIcm cartaIcms2 = null;
cartaIcms2 = (SvdCartaIcm) session.findbypk(SvdCartaIcm.class, cartaIcms.getPk());
assertEquals(cartaIcms.getBairro(), cartaIcms2.getBairro());
}
public void testUpdate() throws HibernateException, RemoteException, NamingException{
cartaIcms.setBairro("Tatuapé");
session.update(cartaIcms);
SvdCartaIcm cartaIcms2 = null;
cartaIcms2 = (SvdCartaIcm) session.findbypk(SvdCartaIcm.class, cartaIcms.getPk());
assertEquals(cartaIcms.getBairro(), cartaIcms2.getBairro());
}
}
Blz ?