ejbFindByPrimaryKeyメソッドは、プライマリキーを返却するメソッドです。
Entity Beanを呼び出すEJBアプリケーションがfindByPrimaryKeyメソッドを呼び出すと、コンテナはfindByPrimaryKeyメソッドに対応するejbFindByPrimaryKeyメソッドを呼び出します。
記述する処理の概要
ejbFindByPrimaryKeyメソッドには、以下の処理を記述します。引数にはプライマリキーオブジェクトを指定します。
引数(プライマリキーオブジェクト)を検索キーにしてプライマリキーを検索(SELECT)する処理
プライマリキーオブジェクトを作成する処理
ejbFindByPrimaryKeyメソッドの規約
ejbFindByPrimaryKeyメソッドは以下の規約を満たしていなければなりません。
メソッドはpublicとして定義されていなければなりません。
返却値はEnterprise Beanのプライマリキータイプでなければなりません。
メソッドの引数は、RMI over IIOPの規約に従わなくてはなりません。
throws句には以下の例外を定義することができます。
任意のEJBアプリケーション固有の例外
javax.ejb.EJBException
javax.ejb.FinderException
javax.ejb.ObjectNotFoundException
記述例
public SampleBeanPrimaryKey ejbFindByPrimaryKey(SampleBeanPrimaryKey SampleBeanPrimaryKeyValue) throws javax.ejb.FinderException, javax.ejb.ObjectNotFoundException, javax.ejb.EJBException { Connection connection = null; PreparedStatement psSelect = null; ResultSet rs = null; int rows = 0; SampleBeanPrimaryKey pk = null; try { // プライマリキーの検索 connection = dataSource.getConnection(); psSelect = connection.prepareStatement("SELECT ID FROM SAMPLESCM.SAMPLETBL WHERE ID = ?"); psSelect.setObject(1,SampleBeanPrimaryKeyValue.code); rs = psSelect.executeQuery(); while(rs.next()) { rows++; // プライマリキーオブジェクトの作成 pk = new SampleBeanPrimaryKey(); pk.code = new Integer(rs.getInt(1)); } } catch(SQLException e) { throw new EJBException(e.getMessage()); } finally { try { if (rs != null) rs.close(); if (psSelect != null) psSelect.close(); if (connection != null) connection.close(); } catch(Exception e) {} } if (rows == 0) { throw new ObjectNotFoundException ("No Record Found"); } else if (rows > 1) { throw new FinderException ("Many Records Found"); } return pk; }