List of usage examples for org.hibernate SessionFactory getCurrentSession
Session getCurrentSession() throws HibernateException;
From source file:org.jboss.pnc.datastore.repositories.DefaultSequenceHandlerRepository.java
License:Open Source License
@Override public void dropSequence(final String sequenceName) { Work work = new Work() { @Override/*from www . ja v a 2 s .co m*/ public void execute(Connection connection) throws SQLException { DialectResolver dialectResolver = new StandardDialectResolver(); Dialect dialect = dialectResolver.resolveDialect(getResolutionInfo(connection)); PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection .prepareStatement(dialect.getDropSequenceStrings(sequenceName)[0]); preparedStatement.execute(); } catch (SQLException e) { throw e; } finally { if (preparedStatement != null) { preparedStatement.close(); } if (resultSet != null) { resultSet.close(); } } } }; Session session = (Session) entityManager.getDelegate(); SessionFactory sessionFactory = session.getSessionFactory(); sessionFactory.getCurrentSession().doWork(work); }
From source file:org.jboss.pnc.datastore.repositories.SequenceHandlerRepository.java
License:Open Source License
public Long getNextID(final String sequenceName) { ReturningWork<Long> maxReturningWork = new ReturningWork<Long>() { @Override/*from www . j a v a 2s . c o m*/ public Long execute(Connection connection) throws SQLException { DialectResolver dialectResolver = new StandardDialectResolver(); Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData()); PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(dialect.getSequenceNextValString(sequenceName)); resultSet = preparedStatement.executeQuery(); resultSet.next(); return resultSet.getLong(1); } catch (SQLException e) { throw e; } finally { if (preparedStatement != null) { preparedStatement.close(); } if (resultSet != null) { resultSet.close(); } } } }; Session session = (Session) entityManager.getDelegate(); SessionFactory sessionFactory = session.getSessionFactory(); Long maxRecord = sessionFactory.getCurrentSession().doReturningWork(maxReturningWork); return maxRecord; }
From source file:org.jboss.pnc.datastore.repositories.SequenceHandlerRepository.java
License:Open Source License
public void createSequence(final String sequenceName) { if (sequenceExists(sequenceName)) { return;//from www .j av a 2 s . c o m } Work work = new Work() { @Override public void execute(Connection connection) throws SQLException { DialectResolver dialectResolver = new StandardDialectResolver(); Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData()); PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection .prepareStatement(dialect.getCreateSequenceStrings(sequenceName, 1, 1)[0]); preparedStatement.execute(); } catch (SQLException e) { throw e; } finally { if (preparedStatement != null) { preparedStatement.close(); } if (resultSet != null) { resultSet.close(); } } } }; Session session = (Session) entityManager.getDelegate(); SessionFactory sessionFactory = session.getSessionFactory(); sessionFactory.getCurrentSession().doWork(work); }
From source file:org.jboss.pnc.datastore.repositories.SequenceHandlerRepository.java
License:Open Source License
public boolean sequenceExists(final String sequenceName) { ReturningWork<Boolean> work = new ReturningWork<Boolean>() { @Override//from w ww . j a va 2 s . c o m public Boolean execute(Connection connection) throws SQLException { DialectResolver dialectResolver = new StandardDialectResolver(); Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData()); PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(dialect.getQuerySequencesString()); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { if (sequenceName.equals(resultSet.getString(1))) { return true; } } } catch (SQLException e) { throw e; } finally { if (preparedStatement != null) { preparedStatement.close(); } if (resultSet != null) { resultSet.close(); } } return false; } }; Session session = (Session) entityManager.getDelegate(); SessionFactory sessionFactory = session.getSessionFactory(); return sessionFactory.getCurrentSession().doReturningWork(work); }
From source file:org.jboss.pnc.datastore.repositories.SequenceHandlerRepository.java
License:Open Source License
public void dropSequence(final String sequenceName) { Work work = new Work() { @Override//from ww w.ja va 2s . c om public void execute(Connection connection) throws SQLException { DialectResolver dialectResolver = new StandardDialectResolver(); Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData()); PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection .prepareStatement(dialect.getDropSequenceStrings(sequenceName)[0]); preparedStatement.execute(); } catch (SQLException e) { throw e; } finally { if (preparedStatement != null) { preparedStatement.close(); } if (resultSet != null) { resultSet.close(); } } } }; Session session = (Session) entityManager.getDelegate(); SessionFactory sessionFactory = session.getSessionFactory(); sessionFactory.getCurrentSession().doWork(work); }
From source file:org.jboss.test.hibernate.model.v1.PersonBean.java
License:Open Source License
private Session getSession() { try {// w ww. j a v a2 s . c o m InitialContext ctx = new InitialContext(); String sessionName = (String) ctx.lookup("java:/comp/env/HibernateFactory"); SessionFactory sf = (SessionFactory) ctx.lookup(sessionName); return sf.getCurrentSession(); } catch (HibernateException e) { throw e; } catch (NamingException e) { throw new HibernateException(e); } }
From source file:org.jboss.test.hibernate.ProfileService.java
License:Open Source License
private Session getSession() { try {/*ww w . j av a2 s . com*/ SessionFactory sf = (SessionFactory) Util.lookup(SESSION_FACTORY_NAME, SessionFactory.class); return sf.getCurrentSession(); } catch (HibernateException e) { throw e; } catch (Exception e) { throw new HibernateException("Unable to locate current session", e); } }
From source file:org.jbpm.pvm.internal.wire.descriptor.HibernateSessionDescriptor.java
License:Open Source License
public Object construct(WireContext wireContext) { Environment environment = Environment.getCurrent(); if (environment == null) { throw new WireException("no environment"); }//from w ww. ja v a2 s . c om // get the hibernate-session-factory SessionFactory sessionFactory = null; if (factoryName != null) { sessionFactory = (SessionFactory) wireContext.get(factoryName); } else { sessionFactory = environment.get(SessionFactory.class); } if (sessionFactory == null) { throw new WireException("couldn't find hibernate-session-factory " + (factoryName != null ? "'" + factoryName + "'" : "by type ") + "to open a hibernate-session"); } // open the hibernate-session Session session = null; if (useCurrent) { if (log.isTraceEnabled()) log.trace("getting current hibernate session"); session = sessionFactory.getCurrentSession(); } else if (connectionName != null) { Connection connection = (Connection) wireContext.get(connectionName); if (log.isTraceEnabled()) log.trace("creating hibernate session with connection " + connection); session = sessionFactory.openSession(connection); } else { if (log.isTraceEnabled()) log.trace("creating hibernate session"); session = sessionFactory.openSession(); } StandardTransaction standardTransaction = environment.get(StandardTransaction.class); if (standardTransaction != null) { HibernateSessionResource hibernateSessionResource = new HibernateSessionResource(session); standardTransaction.enlistResource(hibernateSessionResource); } return session; }
From source file:org.jdal.hibernate.HibernateUtils.java
License:Apache License
/** * Initialize Object for use with closed Session. * //from w ww. j a v a 2 s . c o m * @param sessionFactory max depth in recursion * @param obj Object to initialize * @param initializedObjects list with already initialized Objects * @param depth max depth in recursion */ private static void initialize(SessionFactory sessionFactory, Object obj, List<Object> initializedObjects, int depth) { // return on nulls, depth = 0 or already initialized objects if (obj == null || depth == 0) { return; } if (!Hibernate.isInitialized(obj)) { // if collection, initialize objects in collection too. Hibernate don't do it. if (obj instanceof Collection) { initializeCollection(sessionFactory, obj, initializedObjects, depth); return; } sessionFactory.getCurrentSession().buildLockRequest(LockOptions.NONE).lock(obj); Hibernate.initialize(obj); } // now we can call equals safely. If object are already initializated, return if (initializedObjects.contains(obj)) return; initializedObjects.add(obj); // initialize all persistent associaciations. ClassMetadata classMetadata = getClassMetadata(sessionFactory, obj); if (classMetadata == null) { return; // Not persistent object } Object[] pvs = classMetadata.getPropertyValues(obj, EntityMode.POJO); for (Object pv : pvs) { initialize(sessionFactory, pv, initializedObjects, depth - 1); } }
From source file:org.jdal.hibernate.HibernateUtils.java
License:Apache License
/** * Initalize Collection and recurse on elements * //from w w w . jav a 2s . co m * @param sessionFactory the hibernate SessionFactory * @param obj the obj to initilize * @param initializedObjects list with already initialized objects * @param depth max depth in recursion */ private static void initializeCollection(SessionFactory sessionFactory, Object obj, List<Object> initializedObjects, int depth) { Collection<?> collection = (Collection<?>) obj; initializeCollection(collection, sessionFactory.getCurrentSession()); // Initialize elements for (Object o : collection) { initialize(sessionFactory, o, initializedObjects, depth - 1); } }