-->
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.  [ 9 posts ] 
Author Message
 Post subject: which hibernate jar do i need?
PostPosted: Mon Sep 18, 2006 5:35 am 
Beginner
Beginner

Joined: Mon Sep 18, 2006 5:33 am
Posts: 25
Im trying to get some hibernate code to work in my spring app (a dao class) but i keep getting this error saying

The type net.sf.hibernate.type.Type canot be resolved.

And this occurs on the following line:
getHibernateTemplate().find("from Photoinfo pi where pi.photoinfoId=?", id);

Please note i have added hibernate3.jar to my class path

So my question is, how do u fix the error - my feeling is that its looking for some hibernate 2 library, or maybe i need to change my code to use hibernate 3 methods... any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 18, 2006 6:52 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Don't use HibernateTemplate (it has no value that you would ever need) and use the Hibernate API directly. The Hibernate3 API is in org.hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 18, 2006 7:41 am 
Beginner
Beginner

Joined: Mon Sep 18, 2006 5:33 am
Posts: 25
So what method would you suggest i use instead of the following?

getHibernateTemplate().find("from Photoinfo pi where pi.photoinfoId=?", id);

Im a bit clueless in where to look in the api (http://www.hibernate.org/hib_docs/v3/api/).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 18, 2006 7:52 am 
Newbie

Joined: Wed Aug 03, 2005 11:04 am
Posts: 10
You definitly shoudl work through the tutorial first:
http://www.hibernate.org/hib_docs/v3/re ... orial.html

It will explain how you obtain a SessionFactory and a Session from this.

Please rate this post.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 18, 2006 8:26 am 
Newbie

Joined: Mon Sep 18, 2006 4:14 am
Posts: 5
You must get session from sessionfactory and then use Session.createQuery() to create your queries.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 18, 2006 7:21 pm 
Beginner
Beginner

Joined: Mon Sep 18, 2006 5:33 am
Posts: 25
Is there a better way to get a single value from a table? This is what ive done below.

Code:
Session session = HibernateUtil.currentSession();
      session.beginTransaction();
      Query query = session.createQuery("from Photoinfo pi where pi.photoinfoId = :id");
      query.setInteger("id", id);
         
      HibernateUtil.closeSession();
      
      return (Photo)query.list().get(0);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 18, 2006 9:23 pm 
Newbie

Joined: Thu Sep 14, 2006 11:35 am
Posts: 3
What I think you are looking for is.

In your DAO

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

plus have the class extend HibernateDaoSupport.

Then in your applicationContext.xml

<!-- Open and close sessions atomatically -->
<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="sessionManager"/>
</property>
</bean>

You will need to change the ref bean to refer to your sessionFactory but, this should give your HibernateDaoSupport class the session it wants.


Top
 Profile  
 
 Post subject: Better way for getting a single value
PostPosted: Tue Sep 19, 2006 1:52 am 
Beginner
Beginner

Joined: Mon Nov 22, 2004 11:21 am
Posts: 42
To waxydock: If you are sure that the query returns only one row you can use the "uniqueResult()" convenience method:
Code:
return (Photo) query.uniqueResult();


But if the query returns more than one row you get an exception.

Regards,
Georg

_________________
Real programmers confuse Halloween and Christmas because OCT 31 = DEC 25


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 2:48 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Do not use this:

Code:
HibernateUtil.currentSession();


Read http://hibernate.org/42.html

Do not use this:

Code:
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


Read http://hibernate.org/42.html

Then read http://hibernate.org/43.html and all articles linked on those two pages.


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