1

Spring 3でHibernate 4.1.0.Finalを使用しています

私はエンティティクラスに次のものを持っています

@Id
@Column(name = "PROJECT_NO")
@GeneratedValue(strategy=GenerationType.TABLE)
private String projectNumber;

データベース トリガーを使用してテーブルの主キーを設定することはできますか? または、これには CustomGenerator を使用する必要がありますか?

上記を試したところ、次の例外があります

org.hibernate.id.IdentifierGenerationException: Unknown integral data type
for ids : java.lang.String

データベース トリガーにはシーケンスがありません。使用しています

SELECT NVL (MAX (project_no), 0) + 1 FROM projects

編集 1

@GeneratedValue(generator="trig")
@GenericGenerator(name="trig", strategy="select", 
parameters=@Parameter(name="key", value="projectNo"))

上記は次の例外をスローします

Hibernate: select PROJECT_NO from PROJECTS where PROJECT_NO =?

java.lang.NullPointerException
exception in save null
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:645)
    at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:4268)
    at org.hibernate.id.SelectGenerator$SelectGeneratorDelegate.bindParameters(SelectGenerator.java:138)
    at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:84)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2764)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3275)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
4

2 に答える 2

3

The problem is that you're using a String instead of a numeric value. Use a Long instead of a String, and your error will disappear.

AFAIK, you can't use a trigger to populate the ID. Indeed, Hibernate would have to retrieve the generated ID, but since it doesn't have an ID, I don't see how it could read back the row it has just inserted (chicken and egg problem).

You could use your SQL query to get an ID before inserting the row, but this strategy is inefficient, and has a risk of duplicate IDs in case of concurrent inserts. So I wouldn't use this strategy. You tagged your post with Oracle. I suggest you use a sequence. that's what they're for.

于 2013-02-27T16:20:57.613 に答える
2

この時点で、Hibernate3.3のドキュメントページでそれを行うことができます。

選択する

一意のキーで行を選択し、主キーの値を取得することにより、データベーストリガーによって割り当てられた主キーを取得します。

于 2013-02-27T16:20:15.287 に答える