Java Code Examples for javax.persistence.EntityManager#getDelegate()

The following examples show how to use javax.persistence.EntityManager#getDelegate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HibernateExtendedJpaDialect.java    From bamboobsc with Apache License 2.0 6 votes vote down vote up
@Override
 public Object beginTransaction(final EntityManager entityManager, 
 		final TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException {
 	
 	Session session = (Session) entityManager.getDelegate();
 	if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
 		getSession(entityManager).getTransaction().setTimeout(definition.getTimeout());
 	}
 	entityManager.getTransaction().begin();
 	logger.debug("Transaction started");
 	session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
	 logger.debug("The connection instance is " + connection.toString());
	 logger.debug("The isolation level of the connection is " + connection.getTransactionIsolation() 
			 + " and the isolation level set on the transaction is " + definition.getIsolationLevel() );
	 DataSourceUtils.prepareConnectionForTransaction(connection, definition);
}
 	});
 	return prepareTransaction(entityManager, definition.isReadOnly(), definition.getName());
 }
 
Example 2
Source File: PersistenceContextStatefulBean.java    From tomee with Apache License 2.0 6 votes vote down vote up
public void testPropgation() throws TestFailureException {
    if (inheritedDelegate == null) return;
    try {
        try {
            final InitialContext ctx = new InitialContext();
            Assert.assertNotNull("The InitialContext is null", ctx);
            final EntityManager em = (EntityManager) ctx.lookup("java:comp/env/persistence/ExtendedTestContext");
            Assert.assertNotNull("The EntityManager is null", em);

            // call a do nothing method to assure entity manager actually exists
            em.getFlushMode();

            final EntityManager delegate = (EntityManager) em.getDelegate();
            Assert.assertSame("Extended entity manager delegate should be the same instance that was found last time",
                inheritedDelegate,
                delegate);
        } catch (final Exception e) {
            e.printStackTrace();
            Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
        }
    } catch (final AssertionFailedError afe) {
        throw new TestFailureException(afe);
    }
}
 
Example 3
Source File: JpaCmpEngine.java    From tomee with Apache License 2.0 6 votes vote down vote up
private synchronized void registerListener(final EntityManager entityManager) {
    if (entityManager instanceof OpenJPAEntityManagerSPI) {
        final OpenJPAEntityManagerSPI openjpaEM = (OpenJPAEntityManagerSPI) entityManager;
        final OpenJPAEntityManagerFactorySPI openjpaEMF = (OpenJPAEntityManagerFactorySPI) openjpaEM.getEntityManagerFactory();

        if (entityManagerListener == null) {
            entityManagerListener = new OpenJPALifecycleListener();
        }
        openjpaEMF.addLifecycleListener(entityManagerListener, (Class[]) null);
        return;
    }

    final Object delegate = entityManager.getDelegate();
    if (delegate != entityManager && delegate instanceof EntityManager) {
        registerListener((EntityManager) delegate);
    }
}
 
Example 4
Source File: CustomHibernateJpaDialect.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public Object beginTransaction(final EntityManager entityManager,
		final TransactionDefinition definition)
		throws PersistenceException, SQLException, TransactionException {

	Session session = (Session) entityManager.getDelegate();
	if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
		getSession(entityManager).getTransaction().setTimeout(
				definition.getTimeout());
	}

	final TransactionData data = new TransactionData();

	session.doWork(new Work() {
		@Override
		public void execute(Connection connection) throws SQLException {
			Integer previousIsolationLevel = DataSourceUtils
					.prepareConnectionForTransaction(connection, definition);
			data.setPreviousIsolationLevel(previousIsolationLevel);
			data.setConnection(connection);
		}
	});

	entityManager.getTransaction().begin();

	Object springTransactionData = prepareTransaction(entityManager,
			definition.isReadOnly(), definition.getName());

	data.setSpringTransactionData(springTransactionData);

	return data;
}
 
Example 5
Source File: LobbyEntrantDao.java    From soapbox-race with GNU General Public License v2.0 5 votes vote down vote up
public void delByPersona(PersonaEntity entity) {
	EntityManager manager = getManager();
	Session delegate = (Session) manager.getDelegate();
	Query query = delegate.createQuery("DELETE from LobbyEntrantEntity obj WHERE obj.persona = :persona ");
	query.setParameter("persona", entity);
	query.executeUpdate();
}
 
Example 6
Source File: SoapboxDao.java    From soapbox-race with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public List<ISoapBoxEntity> find(ISoapBoxEntity entity) {
	EntityManager manager = ConnectionDB.getManager();
	manager.clear();
	Session sessao = (Session) manager.getDelegate();
	Example example = Example.create(entity);
	example.excludeZeroes();
	Criteria criteria = sessao.createCriteria(entity.getClass());
	criteria.add(example);
	return criteria.list();
}
 
Example 7
Source File: JtaEntityManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
public EntityManager getDelegate() {
    final Timer timer = Op.getDelegate.start(this.timer, this);
    try {
        final EntityManager em = getEntityManager();
        em.getDelegate(); // exception if not open etc... to respect the spec
        return em;
    } finally {
        timer.stop();
    }
}
 
Example 8
Source File: PersistenceContextStatefulBean.java    From tomee with Apache License 2.0 4 votes vote down vote up
public void testPropagatedPersistenceContext() throws TestFailureException {
    try {
        try {
            final InitialContext ctx = new InitialContext();
            Assert.assertNotNull("The InitialContext is null", ctx);
            final EntityManager em = (EntityManager) ctx.lookup("java:comp/env/persistence/ExtendedTestContext");
            Assert.assertNotNull("The EntityManager is null", em);

            // call a do nothing method to assure entity manager actually exists
            em.getFlushMode();

            // get the raw entity manager so we can test it below
            inheritedDelegate = (EntityManager) em.getDelegate();

            // The extended entity manager is not propigated to a non-extended entity manager unless there is a transaction
            final EntityManager nonExtendedEm = (EntityManager) ctx.lookup("java:comp/env/persistence/TestContext");
            nonExtendedEm.getFlushMode();
            final EntityManager nonExtendedDelegate = ((EntityManager) nonExtendedEm.getDelegate());
            Assert.assertTrue("non-extended entity manager should be open", nonExtendedDelegate.isOpen());
            Assert.assertNotSame("Extended non-extended entity manager shound not be the same instance as extendend entity manager when accessed out side of a transactions",
                inheritedDelegate,
                nonExtendedDelegate);

            // When the non-extended entity manager is accessed within a transaction is should see the stateful extended context.
            //
            // Note: this code also tests EBJ 3.0 Persistence spec 5.9.1 "UserTransaction is begun within the method, the
            // container associates the persistence context with the JTA transaction and calls EntityManager.joinTransaction."
            // If our the extended entity manager were not associted with the transaction, the non-extended entity manager would
            // not see it.
            final UserTransaction userTransaction = ejbContext.getUserTransaction();
            userTransaction.begin();
            try {
                Assert.assertSame("Extended non-extended entity manager to be same instance as extendend entity manager",
                    inheritedDelegate,
                    nonExtendedEm.getDelegate());
            } finally {
                userTransaction.commit();
            }

            // When a stateful bean with an extended entity manager creates another stateful bean, the new bean will
            // inherit the extended entity manager (assuming it contains an extended entity manager for the same persistence
            // unit).
            final PersistenceContextStatefulHome home = (PersistenceContextStatefulHome) ejbContext.getEJBHome();
            final PersistenceContextStatefulObject object = home.create();

            // test the new stateful bean recieved the context
            object.testPropgation();

            // remove the bean
            object.remove();
        } catch (final Exception e) {
            Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
        }
    } catch (final AssertionFailedError afe) {
        throw new TestFailureException(afe);
    }
}
 
Example 9
Source File: HibernateUtils.java    From es with Apache License 2.0 2 votes vote down vote up
/**
 * 根据jpa EntityManager 获取 hibernate Session API
 *
 * @param em
 * @return
 */
public static Session getSession(EntityManager em) {
    return (Session) em.getDelegate();
}