下面列出了怎么用javax.ejb.FinderException的API类实例代码及写法,或者点击链接到github查看源代码。
public List<Object> queryBeans(final BeanContext beanContext, final String signature, final 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);
}
}
return executeSelectQuery(query, args);
}
public static float execute_float(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 Number result = (Number) cmpContainer.select(beanContext, methodSignature, "float", args);
return result.floatValue();
}
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;
}
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();
}
public static char execute_char(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 Character result = (Character) cmpContainer.select(beanContext, methodSignature, "char", args);
return result;
}
public static int execute_int(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 Number result = (Number) cmpContainer.select(beanContext, methodSignature, "int", args);
return result.intValue();
}
/**
* Maps to BasicBmp2DataSourcesHome.findByPrimaryKey
*
* @param primaryKey
* @return Integer
* @throws javax.ejb.FinderException
*/
public Integer ejbFindByPrimaryKey(final Integer primaryKey)
throws javax.ejb.FinderException {
boolean found = false;
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 * from entity where id = ?");
try {
stmt.setInt(1, primaryKey.intValue());
found = stmt.executeQuery().next();
} finally {
stmt.close();
}
} finally {
con.close();
}
} catch (final Exception e) {
throw new FinderException("FindByPrimaryKey failed");
}
if (found) return primaryKey;
else throw new javax.ejb.ObjectNotFoundException();
}
/**
* 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();
}
public static long execute_long(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 Number result = (Number) cmpContainer.select(beanContext, methodSignature, "long", args);
return result.longValue();
}
/**
* Select a collection local ejb
*/
public Collection ejbHomeSelectCollectionLocalEjb() throws FinderException {
return ejbSelectCollectionLocalEjb();
}
public int update(final BeanContext beanContext, final String methodSignature, final Object... args) throws FinderException {
final String signature = beanContext.getAbstractSchemaName() + "." + methodSignature;
// exectue the update query
return cmpEngine.executeUpdateQuery(beanContext, signature, args);
}
/**
* Select a single boolean field
*/
public boolean ejbHomeSelectSingleBooleanField(final boolean value) throws FinderException {
return ejbSelectSingleBooleanField(value);
}
/**
* Select a collection int field
*/
public Collection ejbHomeSelectCollectionIntField() throws FinderException {
return ejbSelectCollectionIntField();
}
public AccountDataBean getAccountData(String userID) throws FinderException, RemoteException {
return convertAccountDataBean(getTrade().getAccountData(userID));
}
/**
* Select a single remote ejb
*/
public Object ejbHomeSelectSingleRemoteEjb(final int value) throws FinderException {
return ejbSelectSingleRemoteEjb(value);
}
private PlatformLocal findPlatform(final int platformId) throws FinderException {
return platformLocalHome.findByPrimaryKey(new PlatformPk(platformId, "value" + platformId));
}
/**
* Select a collection long field
*/
public Collection ejbHomeSelectCollectionLongField() throws FinderException {
return ejbSelectCollectionLongField();
}
/**
* Select a single long field
*/
public long ejbHomeSelectSingleLongField(final long value) throws FinderException {
return ejbSelectSingleLongField(value);
}
public ALocal ejbHomeSelectTest(final String test) throws FinderException {
return ejbSelectTest(test);
}
private PersonLocal findPerson(final int personId) throws FinderException {
return personLocalHome.findByPrimaryKey(new PersonPk(personId, "value" + personId));
}
/**
* Select a single float field
*/
public float ejbHomeSelectSingleFloatField(final float value) throws FinderException {
return ejbSelectSingleFloatField(value);
}
public Object select(final BeanContext beanContext, final String methodSignature, final String returnType, final Object... args) throws FinderException {
final String signature = beanContext.getAbstractSchemaName() + "." + methodSignature;
try {
// execute the select query
final Collection<Object> results = cmpEngine.queryBeans(beanContext, signature, args);
//
// process the results
//
// If we need to return a set...
final Collection<Object> proxies;
if (returnType.equals("java.util.Set")) {
// we collect values into a LinkedHashSet to preserve ordering
proxies = new LinkedHashSet<>();
} else {
// otherwise use a simple array list
proxies = new ArrayList<>();
}
final boolean isSingleValued = !returnType.equals("java.util.Collection") && !returnType.equals("java.util.Set");
ProxyFactory proxyFactory = null;
for (Object value : results) {
// if this is a single valued query and we already have results, throw FinderException
if (isSingleValued && !proxies.isEmpty()) {
throw new FinderException("The single valued query " + methodSignature + "returned more than one item");
}
// if we have an EntityBean, we need to proxy it
if (value instanceof EntityBean) {
final EntityBean entityBean = (EntityBean) value;
if (proxyFactory == null) {
final BeanContext result = getBeanContextByClass(entityBean.getClass());
if (result != null) {
proxyFactory = new ProxyFactory(result);
}
}
if (proxyFactory != null) {
if (beanContext.isRemoteQueryResults(methodSignature)) {
value = proxyFactory.createRemoteProxy(entityBean, this);
} else {
value = proxyFactory.createLocalProxy(entityBean, this);
}
}
}
proxies.add(value);
}
// if not single valued, return the set
if (!isSingleValued) {
return proxies;
}
// single valued query that returned no rows, is an exception
if (proxies.isEmpty()) {
throw new ObjectNotFoundException();
}
// return the single item.... multiple return values was handled in for loop above
return proxies.iterator().next();
} catch (final RuntimeException e) {
throw new EJBException(e);
}
}
/**
* Select a collection remote ejb
*/
public abstract Collection ejbSelectCollectionRemoteEjb(String test) throws FinderException;
/**
* Select a single short field
*/
public abstract short ejbSelectSingleShortField(short value) throws FinderException;
/**
* Select a single char field
*/
public abstract char selectSingleCharField(char value) throws FinderException, RemoteException;
/**
*
*/
test.TestingEntityLocal findByPrimaryKey(java.lang.String key) throws javax.ejb.FinderException;
/**
* Select a collection boolean field
*/
public abstract Collection selectCollectionBooleanField() throws FinderException, RemoteException;
/**
*
*/
test.TestingEntityRemote findByPrimaryKey(java.lang.String key) throws javax.ejb.FinderException, java.rmi.RemoteException;
/**
* Select a collection short field
*/
public abstract Collection ejbSelectCollectionShortField(short test) throws FinderException;
/**
* Select a collection char field
*/
public abstract Collection selectCollectionCharField() throws FinderException, RemoteException;