-->
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.  [ 4 posts ] 
Author Message
 Post subject: Two problems using Hibernate
PostPosted: Wed Nov 03, 2004 3:46 pm 
Newbie

Joined: Wed Nov 03, 2004 8:32 am
Posts: 3
Hello,

I'm new to Hibernate and I want to use it for my new project. I have written some very simple tests with it. I have two problems:

1. I have an entity with an unique constraint (<unique = true>) and when I insert a duplicate record Hibernate raise a JDBCException. How do I make the diference between the UNIQUE CONSTRAINT VIOLATION and other exceptions? My code looks like this:

Code:
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
try
{
  session.saveOrUpdate(user);
  HibernateUtil.commitTransaction();
}
catch
{
//
}
finally
{
  HibernateUtil.closeSession();
}


2. I use DAO for loading/saving objects with hibernate. In every method I have HibernateUtil.getSession(); and HibernateUtil.closeSession(). My problem is when I call a DAO method from another DAO method. In the first method I open the session and the second method closes the session. (because I have HibernateUtil.getSession() and HibernateUtil.closeSession() in every method). The second method is called directly from the user interface, so I can't remove HibernateUtil.getSession() and HibernateUtil.closeSession(). I want the method who opens the session to close the session. Any ideea about how can I make it work?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 7:09 pm 
Beginner
Beginner

Joined: Wed Nov 19, 2003 6:46 pm
Posts: 41
Location: Auckland, New Zealand
Regarding your 2nd questions, take a look at the ThreadLocal pattern or the Spring framework (this is really the best solution - Spring and Hibernate together just sing!)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 04, 2004 7:24 am 
Newbie

Joined: Thu Oct 28, 2004 6:57 am
Posts: 2
Location: bangalore india
Hi,

To identify database constraints violation you will need to get the errcode from the JDBCException. This error code canl be specify to the DB vendor.

_________________
- regards
Seemanto B.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 04, 2004 9:05 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
For Question 1.

You should make sure, that this never happens. See http://forum.hibernate.org/viewtopic.php?t=933495.

HTH
Ernst


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