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();
}
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();
}
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);
}
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;
}
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();
}
Aggregations