Interstage Application Server J2EE ユーザーズガイド
目次 索引 前ページ次ページ

第3部 EJB編> 第12章 Entity Beanの実装> 12.6 BMPのEnterprise Beanクラスの作成

12.6.7 ejbRemoveメソッドの記述

 コンテナがデータベースからデータを削除するときに呼び出すメソッドです。

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

■ 記述する処理の概要

 ejbRemoveメソッドには、以下の処理を記述します。

■ ejbRemoveメソッドの規約

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

■ 記述例

public void ejbRemove() throws javax.ejb.EJBException, javax.ejb.RemoveException
    {
        Connection connection = null;
        PreparedStatement psDelete = null;
        SampleBeanPrimaryKey pk = null;
        int rows = 0;
        try
        {
            // レコードの削除
            connection = dataSource.getConnection();
            psDelete = connection.prepareStatement("DELETE FROM SAMPLESCM.SAMPLETBL WHERE ID = ?");
            pk = (SampleBeanPrimaryKey)context.getPrimaryKey();
            psDelete.setObject(1,pk.code);
            rows = psDelete.executeUpdate();
        }
        catch(SQLException e)
        {
            throw new EJBException(e.getMessage());
        }
        finally
        {
            try
            {
                if (psDelete != null)
                    psDelete.close();
                if (connection != null)
                    connection.close();
            }
            catch(Exception e) {}
        }
        if (rows != 1)
            throw new RemoveException("ejbRemove failed");
    }

目次 索引 前ページ次ページ

All Rights Reserved, Copyright(C) 富士通株式会社 2005