You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by beyaNet Consultancy <be...@ntlworld.com> on 2004/02/03 16:14:05 UTC
Accessing Hibernate Class + Session Issues
Hi,
I have 2 related questions which I would like to throw into the arena
if I may.
1. I have created a user class in Hibernate which I return into XSP as
an object like so:
public Object getUserdetails (int user_id)
{
Integer userID = new Integer(user_id);
Object userDetails;
try {
HibernateUtil Util = new HibernateUtil();
Session session = Util.currentSession();
Transaction transaction = session.beginTransaction();
//Get User details
User user = (User)session.load(test.User.class, userID);
//Return user details object
userDetails = user;
transaction.commit();
Util.closeSession();
} catch (HibernateException e) {
throw new RuntimeException("Exception in Hibernate:: " +
e.getMessage(), e);
}
return userDetails;
}
As the User class getters are populated with user information, from
with in the class I can do user.getFirstName() to retrieve the users
firstname.
In my XSP page I read the object in as:
User2 usr2 = new User2();
Object userDetails = usr2.getUserdetails(42);
The problem arises when I try to access the object properties which I
tried as :
String s = userDetails.getFirstName();
which gives me a language exception error: //start error (lines
171-171) "The method getFirstName() is undefined for the type Object"
String s = userDetails.getFirstName(); // end error
How do I access the object to retrieve the user details?
2. Persisting the object in a Session.
Could I use session.setAttribute("userDetails", object)? If so, how
would I access the object properties in this instance?
many thanks in advance
Re: Accessing Hibernate Class + Session Issues
Posted by beyaNet Consultancy <be...@ntlworld.com>.
Nino,
all is fine now. thanks for your help ;-)
On 3 Feb 2004, at 15:25, Nuno Santos wrote:
> You need to type cast to access the methods. Try :
> ((UserDetail)userDetails).getFirstName()
>
> On Tue, 2004-02-03 at 15:14, beyaNet Consultancy wrote:
>> Hi,
>>
>> I have 2 related questions which I would like to throw into the arena
>> if I may.
>>
>>
>> 1. I have created a user class in Hibernate which I return into XSP as
>> an object like so:
>>
>>
>> <color><param>7676,0F0F,5050</param>public</color> Object
>> getUserdetails (<color><param>7676,0F0F,5050</param>int</color>
>> user_id)
>>
>> {
>>
>>
>>
>> Integer userID = <color><param>7676,0F0F,5050</param>new</color>
>> Integer(user_id);
>>
>> Object userDetails;
>>
>>
>>
>> <color><param>7676,0F0F,5050</param>try</color> {
>>
>> HibernateUtil Util =
>> <color><param>7676,0F0F,5050</param>new</color> HibernateUtil();
>>
>> Session session = Util.currentSession();
>>
>>
>> Transaction transaction = session.beginTransaction();
>>
>>
>>
>> <color><param>2323,6E6E,2525</param>//Get User details</color>
>>
>> User user =
>> (User)session.load(test.User.<color><param>7676,0F0F,5050</
>> param>class</color>,
>> userID);
>>
>>
>>
>> <color><param>2323,6E6E,2525</param>//Return user details
>> object</color>
>>
>> userDetails = user;
>>
>>
>>
>> transaction.commit();
>>
>> Util.closeSession();
>>
>>
>>
>> } <color><param>7676,0F0F,5050</param>catch</color>
>> (HibernateException e) {
>>
>> <color><param>7676,0F0F,5050</param>throw</color>
>> <color><param>7676,0F0F,5050</param>new</color>
>> RuntimeException(<color><param>8989,1313,1515</param>"Exception in
>> Hibernate:: "</color> + e.getMessage(), e);
>>
>> }
>>
>>
>>
>> <color><param>7676,0F0F,5050</param>return</color> userDetails;
>>
>> }
>>
>>
>> As the User class getters are populated with user information, from
>> with in the class I can do user.getFirstName() to retrieve the users
>> firstname.
>>
>>
>> In my XSP page I read the object in as:
>>
>>
>> User2 usr2 = new User2();<fixed>
>>
>> </fixed>Object userDetails =
>> usr2.getUserdetails(<color><param>0000,0000,FFFF</param>42</color>);
>>
>>
>> The problem arises when I try to access the object properties which I
>> tried as :
>>
>>
>> String s = userDetails.getFirstName();
>>
>>
>> which gives me a language exception error: //start error (lines
>> 171-171) "The method getFirstName() is undefined for the type Object"
>> String s = userDetails.getFirstName(); // end error
>>
>>
>> How do I access the object to retrieve the user details?
>>
>>
>> 2. Persisting the object in a Session.
>>
>>
>> Could I use session.setAttribute("userDetails", object)? If so, how
>> would I access the object properties in this instance?
>>
>>
>> many thanks in advance
> --
> Nuno Santos <ns...@electroplus.pt>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: Accessing Hibernate Class + Session Issues
Posted by Nuno Santos <ns...@electroplus.pt>.
You need to type cast to access the methods. Try :
((UserDetail)userDetails).getFirstName()
On Tue, 2004-02-03 at 15:14, beyaNet Consultancy wrote:
> Hi,
>
> I have 2 related questions which I would like to throw into the arena
> if I may.
>
>
> 1. I have created a user class in Hibernate which I return into XSP as
> an object like so:
>
>
> <color><param>7676,0F0F,5050</param>public</color> Object
> getUserdetails (<color><param>7676,0F0F,5050</param>int</color>
> user_id)
>
> {
>
>
>
> Integer userID = <color><param>7676,0F0F,5050</param>new</color>
> Integer(user_id);
>
> Object userDetails;
>
>
>
> <color><param>7676,0F0F,5050</param>try</color> {
>
> HibernateUtil Util =
> <color><param>7676,0F0F,5050</param>new</color> HibernateUtil();
>
> Session session = Util.currentSession();
>
>
> Transaction transaction = session.beginTransaction();
>
>
>
> <color><param>2323,6E6E,2525</param>//Get User details</color>
>
> User user =
> (User)session.load(test.User.<color><param>7676,0F0F,5050</param>class</color>,
> userID);
>
>
>
> <color><param>2323,6E6E,2525</param>//Return user details object</color>
>
> userDetails = user;
>
>
>
> transaction.commit();
>
> Util.closeSession();
>
>
>
> } <color><param>7676,0F0F,5050</param>catch</color>
> (HibernateException e) {
>
> <color><param>7676,0F0F,5050</param>throw</color>
> <color><param>7676,0F0F,5050</param>new</color>
> RuntimeException(<color><param>8989,1313,1515</param>"Exception in
> Hibernate:: "</color> + e.getMessage(), e);
>
> }
>
>
>
> <color><param>7676,0F0F,5050</param>return</color> userDetails;
>
> }
>
>
> As the User class getters are populated with user information, from
> with in the class I can do user.getFirstName() to retrieve the users
> firstname.
>
>
> In my XSP page I read the object in as:
>
>
> User2 usr2 = new User2();<fixed>
>
> </fixed>Object userDetails =
> usr2.getUserdetails(<color><param>0000,0000,FFFF</param>42</color>);
>
>
> The problem arises when I try to access the object properties which I
> tried as :
>
>
> String s = userDetails.getFirstName();
>
>
> which gives me a language exception error: //start error (lines
> 171-171) "The method getFirstName() is undefined for the type Object"
> String s = userDetails.getFirstName(); // end error
>
>
> How do I access the object to retrieve the user details?
>
>
> 2. Persisting the object in a Session.
>
>
> Could I use session.setAttribute("userDetails", object)? If so, how
> would I access the object properties in this instance?
>
>
> many thanks in advance
--
Nuno Santos <ns...@electroplus.pt>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org