-->
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.  [ 3 posts ] 
Author Message
 Post subject: Is this the best way?
PostPosted: Tue Aug 03, 2004 10:46 pm 
Beginner
Beginner

Joined: Fri Jan 16, 2004 4:58 pm
Posts: 37
I am pretty new at Hibernate and I got some code working without much effort but wanted to know if this is the best way to go about this.

I have a table where I store issues for a help desk system. I have a JavaBean and the mapping xml file for this table. Part of the issue number I create contains the Auto Incremented ID number from the issue table. So after I insert the new record I get the ID and create the issue number, then I update that row with the new ticket number. Here is the code I am using to do this:

Code:
//Get the Session
Session ses = HibernateUtil.currentSession();
//Create a transaction
Transaction tx = ses.beginTransaction();
//Insert new Record
ses.save(callBean);
//Get current Date
java.util.Date date = new java.util.Date();
//Format the date
SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
//Generate Issue Number
String ticket = techid + sdf.format(date).toString() + String.valueOf(callBean.getId());
//Set the Issue Number in the JavaBean
callBean.setTicket(ticket);
//Update the record with the new Issue Number
ses.saveOrUpdate(callBean);
//Commit the transaction
tx.commit();
//Close the session
HibernateUtil.closeSession();


This works, I just want to make sure what I am doing is not a "work around" way but the proper way of doing things.

Thanks for looking at this.

_________________
Gregg Bolinger
Javaranch Sheriff
Weblog


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 7:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Its OK but you don't need the initial save and you should wrap it in a try catch finally block to make sure you rollback and close the session correctly.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 7:59 pm 
Beginner
Beginner

Joined: Fri Jan 16, 2004 4:58 pm
Posts: 37
Thanks David. It is in a try catch. I just omitted that bit. I bought the E-book of HIA and I read about not needing the initial save and the automatic dirty checking. I just hadn't made it back here to post my findings.

Thanks again.

_________________
Gregg Bolinger
Javaranch Sheriff
Weblog


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