2011-01-23 20 views
2

以下のドメインオブジェクトを持つグループチャットのプロジェクトを作成しています。私が望むのは、CommentPKのorderIdが、提供されたgroupIdに対して自動インクリメントされるということです。 GenerationType.Table、pkColumnName = "max_id_name"、pkColumnValue = "max_comment_id"、valueColumnName = "max_id"のようなメソッドを使用すると、orderIdはグローバルに一意であり、システムがキーとしてサポートするコメントの最大数を制限します "max_comment_id "は定数です。代わりに、私は、例えば、与えられたgroupIdから派生するpkColumnValueを使用したいと思います。グループ1ではキーがmax_comment_group_1であり、グループ2ではキーが「max_comment_group_2」であるとします。 @TableGeneratorのpkColumnValueフィールドに変数値を与えることができるように、JPA仕様のみを使用したいと思います。しかし、任意の休止状態の特定の解決策も行うでしょう。Hibernate JPA複合キー自動生成オプション

@Entity 
class Group 
{ 
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) 
long id; 
String name; 
} 

@Entity 
class User 
{ 
@TableGenerator(
     name="max_ids_generator", 
     table="max_ids", 
     pkColumnName="max_id_name", 
     pkColumnValue="max_user_id", 
     valueColumnName="max_id", 
     initialValue=1, 
     allocationSize=1 
    ) 

@Id 
@GeneratedValue(strategy=GenerationType.TABLE, generator="max_ids_generator") 
long id; 
long name; 
} 

@Embeddable 
class CommentPK 
{ 
long groupId; 
long orderId; // this is the ordering of the comment, Eg. 1st comment, 2nd comment for the provided groupId. 
} 

@Entity 
class Comment 
{ 
@EmbeddedId 
CommentPK commentId; 
long userId; 
String comment; 
} 

ありがとうございます。

+0

誰でもお手伝いできますか? – nattu

+0

コメントが削除されていない限り、「注文のコメント」は連続していなければなりませんか? –

+0

これに対する回答/解決方法がありますか? – javydreamercsw

答えて

関連する問題