我正在为具有以下域对象的群聊制作一个项目。我想要的是 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”。我只想为此使用 JPA 规范,以便我可以为 @TableGenerator 中的 pkColumnValue 字段提供变量值。
@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;
}
提前致谢。