4

私はSpring 3.0.2を使用しており、JDBCを使用してdbを処理するMovieDAOというクラスがあります。@Repository アノテーションを設定し、SQLException を Spring の DataAccessException に変換したいのですが、次の例があります。

   @Repository
    public class JDBCCommentDAO implements CommentDAO {

        static JDBCCommentDAO instance;
        ConnectionManager connectionManager;

        private JDBCCommentDAO() {
            connectionManager = new ConnectionManager("org.postgresql.Driver", "postgres", "postgres");
        }

        static public synchronized JDBCCommentDAO getInstance() {
            if (instance == null)
                instance = new JDBCCommentDAO();
            return instance;
        }

        @Override
        public Collection<Comment> getComments(User user) throws DAOException {
            Collection<Comment> comments = new ArrayList<Comment>();
            try {
                String query = "SELECT * FROM Comments WHERE Comments.userId = ?";
                Connection conn = connectionManager.getConnection();
                PreparedStatement stmt = conn.prepareStatement(query);
                stmt = conn.prepareStatement(query);
                stmt.setInt(1, user.getId());
                ResultSet result = stmt.executeQuery();
                while (result.next()) {
                    Movie movie = JDBCMovieDAO.getInstance().getLightMovie(result.getInt("movie"));
                    comments.add(new Comment(result.getString("text"), result.getInt("score"), user, result.getDate("date"), movie));
                }
                connectionManager.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
                        //CONVERT TO DATAACCESSEXCEPTION
            }
            return comments;
        }
}

Translator を取得する方法がわかりません。また、Spring クラスを拡張したくありません。それが @Repository アノテーションを使用している理由です。

4

3 に答える 3

4

目標を達成するには、Beanポストプロセッサを提供する必要があります

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

またはSQLExceptionSubclassTranslatorを使用します

private SQLExceptionTranslator sqlExceptionTranslator = new SQLExceptionSubclassTranslator();

catch(SQLException e) {
    throw sqlExceptionTranslator.doTranslate("<WHICH_TASK>", "<WHICH_SQL_QUERY>", e);
}

代わりは

于 2010-05-09T01:15:22.750 に答える
1

@RepositoryJDBC は実行時例外ではないため、自動例外変換ではうまく機能しませんSQLException@Repository-style 例外変換は、Hibernate や JPA などのランタイム例外を使用するデータ アクセス API でのみ実際に機能します。

@Repository注釈は、Spring コンテキストによって使用され、DAO の周りにプロキシ ラッパーを自動生成し、スローされる例外を変換します。ただし、これはランタイム例外でのみ機能します。具体的には、DAO 実装クラス メソッドが をスローする場合SQLException、インターフェイス メソッド シグネチャもプロキシ ラッパーもスローする必要があるため、クライアント コードはその例外を処理する必要があり、例外変換のポイントが無効になります。

JDBC の場合、通常、Spring API への結合が必要です。これには、 を拡張JdbcDaoSupportして使用するか、独自のインスタンスgetExceptionTranslator()を手動で構築する必要があります。SQLExceptionTranslatorいずれにせよSQLException、DAO 内でキャッチして、DataAccessException.

于 2010-05-09T11:25:34.893 に答える
0

キャッチ (SQLException e) {

                    throw new DataAccessException("some message",e);
        }
于 2015-07-06T07:39:55.550 に答える