-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate JPA composite key auto generation option
PostPosted: Sun Jan 23, 2011 3:38 am 
Newbie

Joined: Sun Jan 23, 2011 3:12 am
Posts: 1
Hi everybody,
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.
Code:
@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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.