-->
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: How to define and get ID-Generator for manual ID generation
PostPosted: Fri Dec 28, 2007 6:59 am 
Newbie

Joined: Fri Dec 28, 2007 6:39 am
Posts: 2
Hibernate version:

3.2.5.ga

Name and version of the database you are using:

MySQL 5.x


Hi,

I'm working on a review system and would like to fill a custom ID field (not the primary key) only if the review gets the status "verified".

Can I solve this problem via a hibernate ID-Generator?

If yes, how do I get a previously defined id generator (via hibernate annotations) from a hibernate session?

Are there any better solutions to solve the problem?

Thank you in advance and regards,
Tobias


Top
 Profile  
 
 Post subject: Better Solution
PostPosted: Sat Dec 29, 2007 9:21 am 
Beginner
Beginner

Joined: Thu Jun 21, 2007 9:24 pm
Posts: 20
Location: Lansing, Michigan, USA
You could try instantiating an org.hibernate.id.TableGenerator object directly; but doing it in your object design is probably better.

First, make a new class VerifiedMark:

Code:
class VerifiedMark { // might not need to be public
  int id;
}


and a mapping for it:

Code:
  <class name="VerifiedMark" >
    <id access="field" ><generator class="native" /></id>
  </class>


Now add a property to your "Review" class, and modify the setter for the "status" property:

Code:
.....
  private VerifiedMark verifiedMark;

  public void setStatus(String status) {
    this.status = status;
    if ("Verified".equals(status) && verifiedMark==null)
      verifiedMark = new VerifiedMark();
  }

  public int getVerifiedId() {
    if (verifiedMark==null) return 0; else return verifiedMark.id;
  }
......


I think you would also add a one-to-one mapping from Review to VerifiedMark:

Code:
     <one-to-one name="verifiedMark" access="field" class="...VerifiedMark" />


On MySQL, Hibernate will create one extra table, verifiedmark, with one int primary key auto_increment column. On some other databases Hibernate would use a sequence instead.


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.