-->
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: Using HibernateQuery in EJB 3
PostPosted: Thu May 25, 2006 5:28 pm 
Newbie

Joined: Fri Jan 13, 2006 7:31 pm
Posts: 3
I've been working on an app built on top of JBoss AS 4.0.3sp1/EJB3rc3. I'm in the process of converting it to work with JBoss AS 4.0.4.GA/EJB3rc7.

Most of the changes required have been easily handled thanks to documentation, wiki, and forum posts. Big thanks to all who have contributed!

I am currently stuck on using HibernateQuery instead of the EJB Query and could use some help or direction.

In a number of places, we've preferred to use the HibernateQuery because is returns null for a non-existent entity, instead of throwing an exception (EntityNotFoundException previously, now NoResultException).

We would do this as follows:
Code:
public User getUserByEmail(String email) {
  HibernateQuery hq = (HibernateQuery) em.createQuery(
                "select emailAddress.user from EmailAddress as emailAddress where emailAddress.email = ?1")
      .setParameter(1, email);
  return (User) hq.getHibernateQuery().uniqueResult();
}


However, org.hibernate.ejb.HibernateQuery is no longer available.

I tried the following, but this produces a ClassCastException:
Code:
public User getUserByEmail(String email) {
  org.hibernate.Query hq = (org.hibernate.Query) em
      .createQuery(
          "select emailAddress.user from EmailAddress as emailAddress where emailAddress.email = ?1")
      .setParameter(1, email);
  return (User) hq.uniqueResult();
}


Can someone point me to the proper way to use org.hibernate.Query? Or better yet, point me to a recommended approach to getting the behavior we're after (a single result of null when an entity is not found without having an exception thrown)? I'm trying to avoid the processing of unnecessary throwables.

Thanks for the help and/or guidance,
Jonn


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 3:35 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Since you would 'expose' Hibernate classes anyway - with its Query class - why don't you simply use Session instead of EM?


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.