-->
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.  [ 5 posts ] 
Author Message
 Post subject: Getting the newly generated uuid value
PostPosted: Wed Jan 02, 2008 1:27 pm 
Newbie

Joined: Mon Aug 15, 2005 3:43 am
Posts: 15
Hi all
Happy new year!
I am using the uuid generated field for a class in my project. I need to get value of the generated value after inserting a new row to the database. Could somebody help me accomplish this please?

Thanks
Arash


Top
 Profile  
 
 Post subject: Re: Getting the newly generated uuid value
PostPosted: Wed Jan 02, 2008 6:53 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
arashbi wrote:
Hi all
Happy new year!
I am using the uuid generated field for a class in my project. I need to get value of the generated value after inserting a new row to the database. Could somebody help me accomplish this please?

Thanks
Arash



You will be able to get around this by using @Generated, or its equivalent XML mapping, over the generated field, and after that you will be able to get the generated value from the inserted entity.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 03, 2008 12:57 pm 
Newbie

Joined: Mon Aug 15, 2005 3:43 am
Posts: 15
I am using the same approach, the problem is the field is null after inserting the new entity until you commit the transaction, but I can not commit the transaction at that point; I need to read the generated value before transaction commit


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 03, 2008 1:10 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
arashbi wrote:
I am using the same approach, the problem is the field is null after inserting the new entity until you commit the transaction, but I can not commit the transaction at that point; I need to read the generated value before transaction commit


Try forcing flush to see if that helps. However, if that's not a solution you may want to call a stored procedure to create a value for you and set the value manually. That's how hibernate handles GUIDs for SQLServer.



Farzad-


Top
 Profile  
 
 Post subject: suggestion
PostPosted: Fri Jan 04, 2008 3:10 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
arashbi,

You can use a simple Java class that generates a unique 32-char ID every time it is called, and set the "generator" property of the ID to "assigned".

This simple Java method does that

Code:
    /**
     * returns a unique 32-char ID
     * @return the ID
     */
    public static String getHashableId(){
      String key=new UID().toString();
      key=StringUtils.repeat(key, 3);
      key=key.substring(0, 32);
      key=key.replace(':', '1');
      key=key.replace('-', '2');
      return key;
    }


However, philosophically speaking, the fact that you absolutely need that ID before the object has being committed to the database is troubling. You should not ever need and ID unless it is from a class that had been inserted and has been properly retrieved my means of a query or load. You might want to reconsider your design.

_________________
Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.