我正在为具有以下域对象的群聊进行项目。我想要的是CommentPK的orderId会根据提供的groupId自动递增。如果我使用类似GenerationType.Table的方法,则pkColumnName =“max_id_name”,pkColumnValue =“max_comment_id”,valueColumnName =“max_id”,那么orderId将是全局唯一的,这也将限制系统支持的最大注释数“max_comment_id “是一个常数。相反,我想使用pkColumnValue,它可以从例如提供的groupId派生。对组1来说,关键是max_comment_group_1,对于组2,让该关键字为“max_comment_group_2”。我只想使用这样的JPA规范,我可以为@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;
}
在此先感谢。
任何人都可以帮忙吗? – nattu 2011-02-19 21:48:32