Java Code Examples for javax.persistence.EntityManager.unwrap()

The following are Jave code examples for showing how to use unwrap() of the javax.persistence.EntityManager class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: SqlSauce   File: DbUtils.java   Source Code and License Vote up 6 votes
public static Map<String, Integer> getColumnNameToIndexMap(final String queryString, final EntityManager em) throws SQLException {

        final Session session = em.unwrap(Session.class); // ATTENTION! This is Hibernate-specific!
        final AtomicReference<ResultSetMetaData> msRef = new AtomicReference<>();
        session.doWork((c) -> {
            try (final PreparedStatement statement = create(c, queryString)) {
                // I'm not setting parameters here, because I just want to find out about the return values' column names
                msRef.set(statement.getMetaData());
            }
        });
        final ResultSetMetaData metaData = msRef.get();
        // LinkedHashmap preserves order of insertion:
        final Map<String, Integer> columnNameToColumnIndex = new LinkedHashMap<>();
        for (int t = 0; t < metaData.getColumnCount(); ++t) {
            // important, first index in the metadata is "1", the first index for the result array must be "0"
            columnNameToColumnIndex.put(metaData.getColumnName(t + 1), t);
        }
        return columnNameToColumnIndex;
    }
 
Example 2
Project: lams   File: EclipseLinkJpaDialect.java   Source Code and License Vote up 5 votes
@Override
public ConnectionHandle getJdbcConnection(EntityManager entityManager, boolean readOnly)
		throws PersistenceException, SQLException {

	Connection con = entityManager.unwrap(Connection.class);
	return (con != null ? new SimpleConnectionHandle(con) : null);
}
 
Example 3
Project: chimera   File: HibernateUtil.java   Source Code and License Vote up 4 votes
public static org.jpos.ee.DB getDB(EntityManager entityManager, Context ctx)
{
    ProxyFactory factory = new ProxyFactory();
    factory.setSuperclass(org.jpos.ee.DB.class);
    Class aClass = factory.createClass();
    final org.jpos.ee.DB newInstance;
    try
    {
        newInstance = (org.jpos.ee.DB) aClass.newInstance();
    }
    catch (InstantiationException | IllegalAccessException e)
    {
        throw new IllegalStateException("Could not create proxy", e);
    }

    MethodHandler methodHandler = (self, overridden, proceed, args) ->
    {
        if (overridden.getName().equals("session"))
        {
            return entityManager.unwrap(Session.class);
        }
        if (overridden.getName().equals("save"))
        {
            entityManager.persist(args[0]);
            return null;
        }
        if (overridden.getName().equals("saveOrUpdate"))
        {
            return entityManager.merge(args[0]);
        }
        if (overridden.getName().equals("delete"))
        {
            entityManager.remove(args[0]);
            return null;
        }
        else if (Arrays.asList(validMethods).contains(overridden.getName()))
        {
            return proceed.invoke(newInstance, args);
        }
        else
        {
            throw new IllegalStateException(
                "Method " + overridden.getName() + " cannot be accessed from Transaction " +
                "Participant");
        }
    };

    ((ProxyObject) newInstance).setHandler(methodHandler);
    return newInstance;
}
 
Example 4
Project: infobip-spring-data-jpa-querydsl   File: SimpleExtendedQueryDslJpaRepository.java   Source Code and License Vote up 4 votes
private DatabaseMetaData getDatabaseMetaData(EntityManager entityManager) throws SQLException {
    SessionImplementor sessionImplementor = entityManager.unwrap(SessionImplementor.class);
    DatabaseMetaData metaData = sessionImplementor.connection().getMetaData();
    entityManager.close();
    return metaData;
}
 
Example 5
Project: lams   File: HibernateJpaDialect.java   Source Code and License Vote up 4 votes
protected Session getSession(EntityManager em) {
	return em.unwrap(Session.class);
}
 
Example 6
Project: strqlbuilder   File: StrQLBuilder.java   Source Code and License Vote up 4 votes
private <X> TypedQuery<X> createQuery(EntityManager entityManager, Class<X> clazz, boolean pagedCount)
{
	StrQLBuilder oThis = this;
	while (oThis.unionParent != null)
	{
		log.warn("Warning: you are trying to execute a query over an auxiliar instance -used to build union union queries-. Use .endUnion() to remove this message.");
		oThis = oThis.unionParent;
	}
	String sql = oThis.build(pagedCount);
	Map<String, Object> parametersMap = oThis.getParametersMap();

	AbstractEntityManagerImpl abstractEntityManager = entityManager.unwrap(AbstractEntityManagerImpl.class);
	try
	{
		QueryTranslator translator = null;
		boolean fixJpqlCountQuery = !isNative && pagedCount;
		if (fixJpqlCountQuery)
		{
			translator = translator(sql, entityManager);
			sql = "select count(*) from (" + translator.getSQLString() + ") x";
		}
		Query q = createQuery(sql, abstractEntityManager, fixJpqlCountQuery);
		TypedQueryImpl<X> typedQuery = new TypedQueryImpl<X>(q, abstractEntityManager);
		if (fixJpqlCountQuery)
		{
			typedQuery.setPositionParameters(getPositionParametersMap(translator, parametersMap));
		}
		else
		{
			typedQuery.setParameters(parametersMap);
		}
		typedQuery.setAlias(oThis.select.getAliases());
		typedQuery.setTargetClass(clazz);

		return typedQuery;
	}
	catch (HibernateException he)
	{
		throw abstractEntityManager.convert(he);
	}
}