Search in sources :

Example 1 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project che-server by eclipse-che.

the class DBInitializer method setUpInjectionManager.

@Inject
public void setUpInjectionManager(GuiceEntityListenerInjectionManager injManager, EntityManagerFactory emFactory) {
    final AbstractSession session = emFactory.unwrap(AbstractSession.class);
    session.setInjectionManager(injManager);
}
Also used : AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Inject(javax.inject.Inject)

Example 2 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project blaze-persistence by Blazebit.

the class EclipseLinkEntityManagerIntegrator method getRegisteredFunctions.

@Override
public Map<String, JpqlFunction> getRegisteredFunctions(EntityManagerFactory entityManagerFactory) {
    AbstractSession session = entityManagerFactory.unwrap(JpaEntityManagerFactory.class).getDatabaseSession();
    DatabasePlatform platform = session.getPlatform();
    @SuppressWarnings("unchecked") Map<Integer, ExpressionOperator> platformOperators = platform.getPlatformOperators();
    Map<String, JpqlFunction> functions = new HashMap<>(platformOperators.size());
    for (ExpressionOperator op : platformOperators.values()) {
        String name = (String) ExpressionOperator.getPlatformOperatorNames().get(op.getSelector());
        if (name != null) {
            if (op instanceof JpqlFunctionExpressionOperator) {
                functions.put(name.toLowerCase(), ((JpqlFunctionExpressionOperator) op).unwrap());
            } else {
                int selector = op.getSelector();
                // No support for these expressions
                if (selector != ExpressionOperator.Union && selector != ExpressionOperator.UnionAll && selector != ExpressionOperator.Intersect && selector != ExpressionOperator.IntersectAll && selector != ExpressionOperator.Except && selector != ExpressionOperator.ExceptAll) {
                    functions.put(name.toLowerCase(), new ExpressionOperatorJpqlFunction(op));
                }
            }
        }
    }
    // Eclipselink doesn't report all functions..
    functions.put("count", new ExpressionOperatorJpqlFunction(ExpressionOperator.count()));
    functions.put("sum", new ExpressionOperatorJpqlFunction(ExpressionOperator.sum()));
    functions.put("avg", new ExpressionOperatorJpqlFunction(ExpressionOperator.average()));
    functions.put("max", new ExpressionOperatorJpqlFunction(ExpressionOperator.maximum()));
    functions.put("min", new ExpressionOperatorJpqlFunction(ExpressionOperator.minimum()));
    functions.put("stddev", new ExpressionOperatorJpqlFunction(ExpressionOperator.standardDeviation()));
    functions.put("var", new ExpressionOperatorJpqlFunction(ExpressionOperator.variance()));
    return functions;
}
Also used : JpqlFunction(com.blazebit.persistence.spi.JpqlFunction) HashMap(java.util.HashMap) DatabasePlatform(org.eclipse.persistence.platform.database.DatabasePlatform) ExpressionOperator(org.eclipse.persistence.expressions.ExpressionOperator) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 3 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.

the class JPAQuery method processStoredProcedureQuery.

/**
 * INTERNAL:
 * Convert the StoredProc call into a DatabaseQuery.
 */
public DatabaseQuery processStoredProcedureQuery(Session session) {
    DatabaseQuery query = null;
    ClassLoader loader = session.getDatasourcePlatform().getConversionManager().getLoader();
    if (resultClassNames != null) {
        List<SQLResultSetMapping> resultSetMappings = new ArrayList<>();
        for (String resultClass : resultClassNames) {
            SQLResultSetMapping mapping = new SQLResultSetMapping(resultClass);
            EntityResult entityResult = new EntityResult(resultClass);
            mapping.addResult(entityResult);
            resultSetMappings.add(mapping);
        }
        query = StoredProcedureQueryImpl.buildResultSetMappingQuery(resultSetMappings, call, hints, loader, (AbstractSession) session);
    } else if (resultSetMappingNames != null) {
        query = StoredProcedureQueryImpl.buildResultSetMappingNameQuery(resultSetMappingNames, call, hints, loader, (AbstractSession) session);
    } else if (resultClassName != null) {
        Class<?> clazz = session.getDatasourcePlatform().getConversionManager().convertClassNameToClass(resultClassName);
        query = StoredProcedureQueryImpl.buildStoredProcedureQuery(clazz, call, hints, loader, (AbstractSession) session);
    } else {
        // Neither a resultClass or resultSetMapping is specified so place in a temp query on the session.
        if (call.isStoredFunctionCall() || call.isStoredPLSQLProcedureCall()) {
            // If it is a function (plsql or not) or plsql procedure use the data read query.
            query = StoredProcedureQueryImpl.buildStoredProcedureQuery(call, hints, loader, (AbstractSession) session);
        } else {
            // Otherwise use a result set mapping query for stored procedure calls so users can use the execute
            // method on it (JPA 2.1 API). Will return the same result, that is, Object[] in this case.
            query = StoredProcedureQueryImpl.buildResultSetMappingQuery(new ArrayList<>(), call, hints, loader, (AbstractSession) session);
        }
    }
    query.setName(getName());
    return query;
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) SQLResultSetMapping(org.eclipse.persistence.queries.SQLResultSetMapping) ArrayList(java.util.ArrayList) EntityResult(org.eclipse.persistence.queries.EntityResult) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 4 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.

the class StoredProcedureQueryImpl method buildResultRecords.

/**
 * Build the given result set into a list objects. Assumes there is an
 * execute call available and therefore should not be called unless an
 * execute statement was issued by the user.
 */
protected List buildResultRecords(ResultSet resultSet) {
    try {
        AbstractSession session = (AbstractSession) getActiveSession();
        DatabaseAccessor accessor = (DatabaseAccessor) executeCall.getQuery().getAccessor();
        executeCall.setFields(null);
        executeCall.matchFieldOrder(resultSet, accessor, session);
        ResultSetMetaData metaData = resultSet.getMetaData();
        List<AbstractRecord> result = new Vector<>();
        while (resultSet.next()) {
            result.add(accessor.fetchRow(executeCall.getFields(), executeCall.getFieldsArray(), resultSet, metaData, session));
        }
        // The result set must be closed in case the statement is cached and not closed.
        resultSet.close();
        return result;
    } catch (Exception e) {
        setRollbackOnly();
        throw new PersistenceException(e);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) PersistenceException(jakarta.persistence.PersistenceException) Vector(java.util.Vector) PersistenceException(jakarta.persistence.PersistenceException) SQLException(java.sql.SQLException) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) QueryTimeoutException(jakarta.persistence.QueryTimeoutException) LockTimeoutException(jakarta.persistence.LockTimeoutException) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 5 with AbstractSession

use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.

the class StoredProcedureQueryImpl method close.

/**
 * Call this method to close any open connections to the database.
 */
@Override
public void close() {
    if (executeCall != null) {
        DatabaseQuery query = executeCall.getQuery();
        AbstractSession session = query.getSession();
        // Release the accessors acquired for the query.
        for (Accessor accessor : query.getAccessors()) {
            session.releaseReadConnection(accessor);
        }
        try {
            if (executeStatement != null) {
                DatabaseAccessor accessor = (DatabaseAccessor) query.getAccessor();
                accessor.releaseStatement(executeStatement, query.getSQLString(), executeCall, session);
            }
        } catch (SQLException exception) {
            // Catch the exception and log a message.
            session.log(SessionLog.WARNING, SessionLog.CONNECTION, "exception_caught_closing_statement", exception);
        }
    }
    executeCall = null;
    executeStatement = null;
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) SQLException(java.sql.SQLException) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)215 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)52 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)28 ArrayList (java.util.ArrayList)26 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)26 Vector (java.util.Vector)22 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)22 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)21 EntityManager (jakarta.persistence.EntityManager)19 List (java.util.List)18 Session (org.eclipse.persistence.sessions.Session)18 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)15 InvalidObject (org.eclipse.persistence.internal.helper.InvalidObject)14 UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)14 HashMap (java.util.HashMap)12 Map (java.util.Map)12 Collection (java.util.Collection)11 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)11 SQLException (java.sql.SQLException)10 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)9