2

I am making a project for Group Chat which has following domain objects. What I want is that the orderId of CommentPK get auto incremented with respect to the groupId provided. If I use method like GenerationType.Table, pkColumnName="max_id_name", pkColumnValue="max_comment_id", valueColumnName="max_id" then the orderId will be globally unique which will also limit the maximum number of comments the system supports as the key "max_comment_id" is a constant. Instead I want to use a pkColumnValue which can be derived from the groupId supplied eg. say for group 1 the key is max_comment_group_1, for group 2 let the key be "max_comment_group_2". I want to use only JPA specs for this such that I can give variable value for pkColumnValue field in @TableGenerator. But any hibernate specific solution will also do.

@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;
}

Thanks in advance.

nattu
  • 149
  • 2
  • 9

0 Answers0