-->
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.  [ 7 posts ] 
Author Message
 Post subject: Additional Getter in Hibernate Mapping
PostPosted: Tue Mar 10, 2009 9:33 am 
Newbie

Joined: Tue Mar 10, 2009 9:27 am
Posts: 3
Hello!

I am working with hibernate3 and spring3. What i am actually working on is
trying to to add one more public #getSomething() method into my
hibernate annotated mapped class. This method uses the persistent class.

While starting up the container spring fails, cause he does not find any
setter for it, which is - of course - correct.
The problem is that I am not able to rename the field due to jsp conflicts.
I want to use it with ${object.something}....

Any ideas or best practice recommendations? How do you add additional getters for doing some work in the mapped table object?

I tried @Transient but after that I cannot use the persistent object anymore because the session is closed.

thanks in advance,
daniel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 1:28 pm 
Beginner
Beginner

Joined: Mon Apr 02, 2007 8:08 am
Posts: 24
If you want a read only property which can be mapped as SQL, take a look at @Formula.

Nevertheless, @Transient is also a sollution. Your problem is likely to be not preloading it when session is still active.

_________________
--
<a href="http://ideasse.blogspot.com/">Ideas + Software Engineering</a> (English)
<a href="http://iiso.blogspot.com/">Ideas + IngenierĂ­a del Software</a> (Spanish)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 2:58 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I tried @Transient but after that I cannot use the persistent object anymore because the session is closed.


I am more or less 100% sure that the "session is closed" error is not related to the use of @Transient. Annotating the getSomething() method as @Transient is the proper solution if you want it to be ignored by Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 4:36 pm 
Newbie

Joined: Tue Mar 10, 2009 9:27 am
Posts: 3
Thanks for your help.

@Transient sounds for me very interesting, it seems to be the best
annotation to get an additional method into the mapped object.

So what could be the reason that my session is closed? How to find out?

The way I do it is just

Code:
Session session = getSessionFactory().getCurrentSession();
Criteria crit = session.createCriteria(Objects.class);
...criteria.list...


build by spring:
Code:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
...


The exception i get is:
Code:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session


Is there something wrong in my configuration?

Thanks again for your help!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 5:05 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You need to keep the session open until your objects has been initialized. Or, if put the other way, you need to initialize your objects before the session is closed. The code you are showing doesn't give any clue to what happens after you have executed the criteria or were the exception happens. What happens between the critera.list() and the exception?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 6:55 pm 
Newbie

Joined: Tue Mar 10, 2009 9:27 am
Posts: 3
I have a standard Spring MVC System. The object is selected in the DAO
(as mentioned above) and returned to the Controller. The controller just
puts it in the map and in the JSP I iterate over the list and want to call
#getSomething in each object.
So in the View I want to "calculate" some object specific things.
The access to the object (or table) itself is no problem, all data is set correct.

The main problem is that the object has a join to another table, and during this access to underlying tables fails! Cause the seesion is closed and that data is not yet retrieved from hibernate...

So what to do? Or is there a better practice where to put such calculations.
I hope this is generally understandable, the way I described it...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 3:08 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I hope this is generally understandable, the way I described it...


Yes, and no! It seems like a fairly standard setup for a Spring app. If you just google around I think you can find several examples of how to set up Spring so that the session is kept open for the duration of the request. It is commonly known as "open-session-in-view" pattern. Variants of this question is asked frequently in this forum so you should be able to find the answer here as well. I have no Spring experience myself so I am afraid that I can't help you with any details. There is a page on the Hibernate wiki that may help you: http://www.hibernate.org/43.html


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