public boolean inserir(parceiroVO parceiro, empresaVO empresa, Collection colProfiles) {
try {
ParceirosDAO parceirosDAO = new ParceirosDAO();
ParceirosEmpresaDAO parceiroEmpresaDAO = new ParceirosEmpresaDAO();
ParceirosProfileDAO parceiroProfileDAO = new ParceirosProfileDAO();
// Inserir Parceiro
logger.debug("Inserir Parceiro");
parceiro.setVchsenhaftp("definir");
logger.debug("Inserir Empresa do Parceiro");
// Inserir Empresa do Parceiro
parceiroEmpresaVOPK parceiroEPK = new parceiroEmpresaVOPK();
parceiroEmpresaVO parceiroE = new parceiroEmpresaVO();
parceiroEPK.setIdEmpresa(empresa.getIdEmpresa());
parceiroEPK.setIdParceiro(parceiro.getIdParceiro());
parceiroE.setComp_id(parceiroEPK);
// parceiroEmpresaDAO.makePersistent(parceiroE);
Set parceirosE = new HashSet();
parceirosE.add(parceiroE);
parceiro.setParceiroempresavo(parceirosE);
// Inserir Profiles do Parceiro
logger.debug("Inserir Profiles do Parceiro");
Iterator it = colProfiles.iterator();
Set parceirosP = new HashSet();
while (it.hasNext()) {
profileVO profile = (profileVO) it.next();
parceiroProfileVOPK parceiroPPK = new parceiroProfileVOPK();
parceiroProfileVO parceiroP = new parceiroProfileVO();
parceiroPPK.setIdProfile(profile.getIdProfile());
parceiroPPK.setIdParceiro(parceiro.getIdParceiro());
parceiroP.setComp_id(parceiroPPK);
// parceiroProfileDAO.makePersistent(parceiroP);
parceirosP.add(parceiroP);
}
parceiro.setParceiroprofilevo(parceirosP);
parceirosDAO.makePersistent(parceiro);
logger.debug("Fazendo Commit das alteracoes");
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
return true;
} catch (Exception e) {
logger.debug("ParceirosBO.inserir ERRO =" + e);
return false;
}
}
package com.bzo2.lupo.utils;
import java.net.URL;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Interceptor;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.bzo2.lupo.exceptions.DAOException;
/**
* Basic Hibernate helper class, handles SessionFactory, Session and Transaction.
* <p>
* Uses a static initializer for the initial SessionFactory creation
* and holds Session and Transactions in thread local variables. All
* exceptions are wrapped in an unchecked InfrastructureException.
*
* @author
[email protected] */
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static Configuration configuration;
private static SessionFactory sessionFactory;
private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();
private static final ThreadLocal threadInterceptor = new ThreadLocal();
// Create the initial SessionFactory from the default configuration files
static {
try {
//configuration = new Configuration();
//sessionFactory = configuration.configure().buildSessionFactory();
URL url = HibernateUtil.class.getResource("/hibernate.cfg.xml");
sessionFactory = new Configuration().configure(url).buildSessionFactory();
// We could also let Hibernate bind it to JNDI:
// configuration.configure().buildSessionFactory()
} catch (Throwable ex) {
// We have to catch Throwable, otherwise we will miss
// NoClassDefFoundError and other subclasses of Error
log.error("Building SessionFactory failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
/**
* Returns the SessionFactory used for this static class.
*
* @return SessionFactory
*/
public static SessionFactory getSessionFactory() {
/* Instead of a static variable, use JNDI:
SessionFactory sessions = null;
try {
Context ctx = new InitialContext();
String jndiName = "java:hibernate/HibernateFactory";
sessions = (SessionFactory)ctx.lookup(jndiName);
} catch (NamingException ex) {
throw new InfrastructureException(ex);
}
return sessions;
*/
return sessionFactory;
}
/**
* Returns the original Hibernate configuration.
*
* @return Configuration
*/
public static Configuration getConfiguration() {
return configuration;
}
/**
* Rebuild the SessionFactory with the static Configuration.
*
*/
public static void rebuildSessionFactory()
throws DAOException {
synchronized(sessionFactory) {
try {
sessionFactory = getConfiguration().buildSessionFactory();
} catch (Exception ex) {
throw new DAOException(ex);
}
}
}
/**
* Rebuild the SessionFactory with the given Hibernate Configuration.
*
* @param cfg
*/
public static void rebuildSessionFactory(Configuration cfg)
throws DAOException {
synchronized(sessionFactory) {
try {
sessionFactory = cfg.buildSessionFactory();
configuration = cfg;
} catch (Exception ex) {
throw new DAOException(ex);
}
}
}
/**
* Retrieves the current Session local to the thread.
* <p/>
* If no Session is open, opens a new Session for the running thread.
*
* @return Session
*/
public static Session getSession()
throws DAOException {
Session s = (Session) threadSession.get();
try {
if (s == null) {
log.debug("Opening new Session for this thread.");
if (getInterceptor() != null) {
log.debug("Using interceptor: " + getInterceptor().getClass());
s = getSessionFactory().openSession(getInterceptor());
} else {
s = getSessionFactory().openSession();
}
threadSession.set(s);
}
} catch (HibernateException ex) {
throw new DAOException(ex);
}
return s;
}
/**
* Closes the Session local to the thread.
*/
public static void closeSession()
throws DAOException {
try {
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null && s.isOpen()) {
log.debug("Closing Session of this thread.");
s.close();
}
} catch (HibernateException ex) {
throw new DAOException(ex);
}
}
/**
* Start a new database transaction.
*/
public static void beginTransaction()
throws DAOException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null) {
log.debug("Starting new database transaction in this thread.");
tx = getSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException ex) {
throw new DAOException(ex);
}
}
/**
* Commit the database transaction.
*/
public static void commitTransaction()
throws DAOException {
Transaction tx = (Transaction) threadTransaction.get();
try {
if ( tx != null && !tx.wasCommitted()
&& !tx.wasRolledBack() ) {
log.debug("Committing database transaction of this thread.");
tx.commit();
}
threadTransaction.set(null);
} catch (HibernateException ex) {
rollbackTransaction();
throw new DAOException(ex);
}
}
/**
* Commit the database transaction.
*/
public static void rollbackTransaction()
throws DAOException {
Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
log.debug("Trying to rollback database transaction of this thread.");
tx.rollback();
}
} catch (HibernateException ex) {
throw new DAOException(ex);
} finally {
closeSession();
}
}
/**
* Reconnects a Hibernate Session to the current Thread.
*
* @param session The Hibernate Session to be reconnected.
*/
public static void reconnect(Session session)
throws DAOException {
try {
session.reconnect();
threadSession.set(session);
} catch (HibernateException ex) {
throw new DAOException(ex);
}
}
/**
* Disconnect and return Session from current Thread.
*
* @return Session the disconnected Session
*/
public static Session disconnectSession()
throws DAOException {
Session session = getSession();
try {
threadSession.set(null);
if (session.isConnected() && session.isOpen())
session.disconnect();
} catch (HibernateException ex) {
throw new DAOException(ex);
}
return session;
}
/**
* Register a Hibernate interceptor with the current thread.
* <p>
* Every Session opened is opened with this interceptor after
* registration. Has no effect if the current Session of the
* thread is already open, effective on next close()/getSession().
*/
public static void registerInterceptor(Interceptor interceptor) {
threadInterceptor.set(interceptor);
}
private static Interceptor getInterceptor() {
Interceptor interceptor =
(Interceptor) threadInterceptor.get();
return interceptor;
}
}