Search in sources :

Example 11 with FinderException

use of javax.ejb.FinderException in project tomee by apache.

the class EmployeeBean method ejbFindByPrimaryKey.

public Integer ejbFindByPrimaryKey(final Integer primaryKey) throws javax.ejb.FinderException {
    boolean found = false;
    try {
        final InitialContext jndiContext = new InitialContext();
        final javax.sql.DataSource ds = (javax.sql.DataSource) jndiContext.lookup("java:comp/env/jdbc/orders");
        final Connection con = ds.getConnection();
        try {
            final PreparedStatement stmt = con.prepareStatement("select * from Employees where EmployeeID = ?");
            try {
                stmt.setInt(1, primaryKey.intValue());
                final ResultSet rs = stmt.executeQuery();
                found = rs.next();
            } finally {
                stmt.close();
            }
        } finally {
            con.close();
        }
    } catch (final Exception e) {
        e.printStackTrace();
        throw new FinderException("FindByPrimaryKey failed");
    }
    if (found)
        return primaryKey;
    else
        throw new javax.ejb.ObjectNotFoundException();
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) InitialContext(javax.naming.InitialContext) FinderException(javax.ejb.FinderException) FinderException(javax.ejb.FinderException) ResultSet(java.sql.ResultSet)

Example 12 with FinderException

use of javax.ejb.FinderException in project tomee by apache.

the class BasicBmpBean method ejbFindByLastName.

/**
     * Maps to BasicBmpHome.findByPrimaryKey
     *
     * @param lastName
     * @return
     * @throws javax.ejb.FinderException
     * @see BasicBmpHome#sum
     */
public java.util.Collection ejbFindByLastName(final String lastName) throws javax.ejb.FinderException {
    final java.util.Vector keys = new java.util.Vector();
    try {
        final InitialContext jndiContext = new InitialContext();
        final DataSource ds = (DataSource) jndiContext.lookup("java:comp/env/jdbc/basic/entityDatabase");
        final Connection con = ds.getConnection();
        try {
            final PreparedStatement stmt = con.prepareStatement("SELECT id FROM entity WHERE last_name = ?");
            try {
                stmt.setString(1, lastName);
                final ResultSet set = stmt.executeQuery();
                while (set.next()) keys.add(new Integer(set.getInt("id")));
            } finally {
                stmt.close();
            }
        } finally {
            con.close();
        }
    } catch (final Exception e) {
        throw new FinderException("FindByPrimaryKey failed");
    }
    if (keys.size() > 0)
        return keys;
    else
        throw new javax.ejb.ObjectNotFoundException();
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) InitialContext(javax.naming.InitialContext) RemoveException(javax.ejb.RemoveException) NoSuchObjectException(java.rmi.NoSuchObjectException) NamingException(javax.naming.NamingException) RemoteException(java.rmi.RemoteException) ApplicationException(org.apache.openejb.test.ApplicationException) EJBException(javax.ejb.EJBException) SQLException(java.sql.SQLException) FinderException(javax.ejb.FinderException) NoSuchEntityException(javax.ejb.NoSuchEntityException) DataSource(javax.sql.DataSource) FinderException(javax.ejb.FinderException) ResultSet(java.sql.ResultSet)

Example 13 with FinderException

use of javax.ejb.FinderException in project tomee by apache.

the class JpaCmpEngine method queryBeans.

public List<Object> queryBeans(final ThreadContext callContext, final Method queryMethod, final Object[] args) throws FinderException {
    final BeanContext deploymentInfo = callContext.getBeanContext();
    final EntityManager entityManager = getEntityManager(deploymentInfo);
    final StringBuilder queryName = new StringBuilder();
    queryName.append(deploymentInfo.getAbstractSchemaName()).append(".").append(queryMethod.getName());
    final String shortName = queryName.toString();
    if (queryMethod.getParameterTypes().length > 0) {
        queryName.append('(');
        boolean first = true;
        for (final Class<?> parameterType : queryMethod.getParameterTypes()) {
            if (!first) {
                queryName.append(',');
            }
            queryName.append(parameterType.getCanonicalName());
            first = false;
        }
        queryName.append(')');
    }
    final String fullName = queryName.toString();
    Query query = createNamedQuery(entityManager, fullName);
    if (query == null) {
        query = createNamedQuery(entityManager, shortName);
        if (query == null) {
            throw new FinderException("No query defined for method " + fullName);
        }
    }
    return executeSelectQuery(query, args);
}
Also used : BeanContext(org.apache.openejb.BeanContext) FinderException(javax.ejb.FinderException) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query)

Example 14 with FinderException

use of javax.ejb.FinderException in project tomee by apache.

the class JpaCmpEngine method executeUpdateQuery.

public int executeUpdateQuery(final BeanContext beanContext, final String signature, Object[] args) throws FinderException {
    final EntityManager entityManager = getEntityManager(beanContext);
    Query query = createNamedQuery(entityManager, signature);
    if (query == null) {
        final int parenIndex = signature.indexOf('(');
        if (parenIndex > 0) {
            final String shortName = signature.substring(0, parenIndex);
            query = createNamedQuery(entityManager, shortName);
        }
        if (query == null) {
            throw new FinderException("No query defined for method " + signature);
        }
    }
    // process args
    if (args == null) {
        args = NO_ARGS;
    }
    for (int i = 0; i < args.length; i++) {
        Object arg = args[i];
        // ejb proxies need to be swapped out for real instance classes
        if (arg instanceof EJBObject) {
            arg = Cmp2Util.getEntityBean((EJBObject) arg);
        }
        if (arg instanceof EJBLocalObject) {
            arg = Cmp2Util.getEntityBean((EJBLocalObject) arg);
        }
        query.setParameter(i + 1, arg);
    }
    final int result = query.executeUpdate();
    return result;
}
Also used : FinderException(javax.ejb.FinderException) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) EJBObject(javax.ejb.EJBObject) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) EJBLocalObject(javax.ejb.EJBLocalObject)

Example 15 with FinderException

use of javax.ejb.FinderException in project tomee by apache.

the class EjbSelect method execute_boolean.

public static boolean execute_boolean(final Object obj, final String methodSignature, final Object... args) throws FinderException {
    final BeanContext beanContext = (BeanContext) obj;
    final Container container = beanContext.getContainer();
    if (!(container instanceof CmpContainer)) {
        throw new FinderException("Deployment is not connected to a CmpContainer " + beanContext.getDeploymentID());
    }
    final CmpContainer cmpContainer = (CmpContainer) container;
    final Boolean result = (Boolean) cmpContainer.select(beanContext, methodSignature, "byte", args);
    return result.booleanValue();
}
Also used : BeanContext(org.apache.openejb.BeanContext) FinderException(javax.ejb.FinderException) CmpContainer(org.apache.openejb.core.cmp.CmpContainer) Container(org.apache.openejb.Container) CmpContainer(org.apache.openejb.core.cmp.CmpContainer)

Aggregations

FinderException (javax.ejb.FinderException)21 BeanContext (org.apache.openejb.BeanContext)14 Container (org.apache.openejb.Container)10 CmpContainer (org.apache.openejb.core.cmp.CmpContainer)10 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 InitialContext (javax.naming.InitialContext)5 EJBException (javax.ejb.EJBException)4 EJBLocalObject (javax.ejb.EJBLocalObject)4 EJBObject (javax.ejb.EJBObject)4 RemoteException (java.rmi.RemoteException)3 ResultSet (java.sql.ResultSet)3 EntityBean (javax.ejb.EntityBean)3 ObjectNotFoundException (javax.ejb.ObjectNotFoundException)3 RemoveException (javax.ejb.RemoveException)3 EntityManager (javax.persistence.EntityManager)3 Query (javax.persistence.Query)3 DataSource (javax.sql.DataSource)3 NoSuchObjectException (java.rmi.NoSuchObjectException)2 SQLException (java.sql.SQLException)2