ページの先頭行へ戻る
Interstage Application Server/Interstage Web Server J2EE ユーザーズガイド

13.6.5 ejbFindByPrimaryKeyメソッドの記述

ejbFindByPrimaryKeyメソッドは、プライマリキーを返却するメソッドです。

Entity Beanを呼び出すEJBアプリケーションがfindByPrimaryKeyメソッドを呼び出すと、コンテナはfindByPrimaryKeyメソッドに対応するejbFindByPrimaryKeyメソッドを呼び出します。

記述する処理の概要

ejbFindByPrimaryKeyメソッドには、以下の処理を記述します。引数にはプライマリキーオブジェクトを指定します。

ejbFindByPrimaryKeyメソッドの規約

ejbFindByPrimaryKeyメソッドは以下の規約を満たしていなければなりません。

記述例

  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;
  }