1

この質問は、ほぼ 2 年前にここで尋ねられたものと似ています。一般的なデータベースの問題が発生したときに、Eclipselink 2.5 からスローされた例外を処理する方法に関する情報を探しています。私が得ているのは醜いorg.springframework.transaction.TransactionSystemExceptionもので、何が失敗したかについての情報を私に与えません。単純なエンティティの例を見てみましょう:

@Entity
class Insect(_name: String) {

  def this() = this(null)

  @Id
  @Column(unique = true)
  var name: String = _name
}

同様に単純なリポジトリ:

@Repository
@Transactional
class InsectDaoImpl extends InsectDao {
  @PersistenceContext
  var entityManager: EntityManager = _

  override def addInsect(insect: Insect) = entityManager.persist(insect)
}

次のコードを実行します。

insectDao.addInsect(new Insect("hornet"))
insectDao.addInsect(new Insect("hornet"))

私に与えます:

org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "CONSTRAINT_INDEX_8 ON PUBLIC.INSECT(NAME)"; SQL statement:
INSERT INTO INSECT (NAME) VALUES (?) [23505-172]
Error Code: 23505
Call: INSERT INTO INSECT (NAME) VALUES (?)
    bind => [1 parameter bound]
Query: InsertObjectQuery(pl.zientarski.model.Insect@1df202be)

ヤッ!例外自体は、問題の原因について建設的なことを何も述べていません。内部例外は、まず第一にデータベース固有のものであり、次に例外メッセージだけが何が問題なのかを説明しています。比較のために、Hibernate を使用した同じセットアップでは、次の例外が発生します。

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["PRIMARY_KEY_8 ON PUBLIC.INSECT(NAME)"; SQL statement:
insert into Insect (name) values (?) [23505-172]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

例外タイプは、何が起こっているかについてのヒントを即座に提供します。

まず、 Eclipselinkorg.springframework.orm.jpa.vendor.EclipseLinkJpaDialect.translateExceptionIfPossible(RuntimeException ex)固有の例外をサポートするためにオーバーライドされていません。次に、から継承されたデフォルトの実装ではDefaultJpaDialectjavax.persistence.PersistenceException.

この問題を回避するためのベスト プラクティスを教えてください。あるいは、そこにある解決策が見えないだけかもしれません。あなたの提案を教えてください。

テストはEclipselink 2.5およびSpring 3.2.3で行われます

ありがとう。

4

0 に答える 0