-->
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.  [ 2 posts ] 
Author Message
 Post subject: Assigned id - Performance Question
PostPosted: Tue Feb 27, 2007 4:45 am 
Newbie

Joined: Thu Dec 07, 2006 9:48 am
Posts: 18
Hi..

I need to assign a calculated id to entities, but I'm not sure what is the best practice.

ID format :
[yy][ItemNo][ItemType]

yy: years 07, 08 etc. Fixed width: 2 chars
ItemNo: item no: incremented or from user input. Starting for 0 for each year. Fixed width: 6 chars.
ItemType: item type code. Fixed width: 2 chars. (There can be several items with same ItemNo)

For example,
0700002511 > 2007, Item no: 25, item type: 11
0700002512 > 2007, Item no: 25, item type: 12
0700002601 > 2007, Item no: 26, item type: 1

Current problems:
ID may be exist, because users can manually enters.
ItemNo must be resetted every year.
What if more than one user saves a record in same time?
Any ideas how to implement ?

thanks!


Hibernate version:
3.2.2


Last edited by GBerberoglu on Tue Feb 27, 2007 6:44 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 6:07 am 
Newbie

Joined: Thu Dec 07, 2006 9:48 am
Posts: 18
This is my current solution:

Code:
      Session session = HibernateSessionFactory.getSession();
      Item item = new Item();
      Integer itemNo = (Integer) session.createSQLQuery("select NextID from temp").list().get(0);
      String year = "07";    // TODO: hardcoded

      String id = year + String.format("%06d", itemNo);
      while (session.createSQLQuery("select ID from item where ID = '" + id + "'").list().size() > 0)
         id = "07" + String.format("%06d", ++itemNo);

      item.setId(id);
      session.save(item);
      session.createSQLQuery("update temp set NextID = " + ++itemNo).executeUpdate();
      
      session.flush();
      session.close();


please comment on it!


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

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.