次のドメイン オブジェクトを持つグループ チャットのプロジェクトを作成しています。私が欲しいのは、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 仕様のみを使用したいと考えています。
@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;
}
前もって感謝します。