-->
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.  [ 3 posts ] 
Author Message
 Post subject: Need help with Hibernate
PostPosted: Tue Jan 16, 2007 8:06 am 
Newbie

Joined: Tue Jan 16, 2007 7:54 am
Posts: 6
Hi friends,

I am new to hibernate and am facing a very weird problem.

My User.hbm.xml is

<class name="com.hp.ts.bean.User" table="User">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="firstname" type="string" column="FIRSTNAME"/>
<property name="lastname" type="string" column="LASTNAME"/>
<property name="username" type="string" column="USERID"/>
<property name="password" type="string" column="PASSWORD"/>
<property name="address" type="string" column="ADDRESS"/>
<property name="zipcode" type="string" column="ZIPCODE"/>
<property name="phonenumber" type="string" column="PHONENUMBER"/>

</class>


My table is -
mysql> desc user
-> ;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| ID | bigint(20) | | | 0 | |
| FIRSTNAME | varchar(255) | YES | | NULL | |
| LASTNAME | varchar(255) | YES | | NULL | |
| USERID | varchar(255) | YES | | NULL | |
| PASSWORD | varchar(255) | YES | | NULL | |
| ADDRESS | varchar(255) | YES | | NULL | |
| ZIPCODE | varchar(255) | YES | | NULL | |
| PHONENUMBER | varchar(255) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)

And When I do something like this in the the listUser() method:-

System.out.println("before Session ");
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
System.out.println("After session " + session);
session.beginTransaction();
System.out.println("After transaction");
String SQL = "select ID,FIRSTNAME,LASTNAME,USERID,PASSWORD,ADDRESS,ZIPCODE,PHONENUMBER from User";
query = session.createSQLQuery(SQL);
result = query.list();
System.out.println("======================");
System.out.println( "Result: "+result.size() );
System.out.println( "Result.get 0: "+result.get(0));
System.out.println( "Result.get 0: "+((User)result.get(0)).getAddress()); --> I get a ClassCastException here
/*Iterator iter = result.iterator();



I get the following exception:-

17:18:24,532 ERROR [STDERR] java.lang.ClassCastException: [Ljava.lang.Object;
17:18:24,532 ERROR [STDERR] at com.hp.ts.manager.UserManager.listUsers(Unknown Source)
17:18:24,532 ERROR [STDERR] at com.hp.ts.action.LoginAction.execute(Unknown Source)
17:18:24,532 ERROR [STDERR] at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
17:18:24,532 ERROR [STDERR] at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
17:18:24,532 ERROR [STDERR] at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
17:18:24,532 ERROR [STDERR] at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
17:18:24,532 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
17:18:24,532 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
17:18:24,532 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:25
2)


Please suggest.
Thanks
Vishal

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 8:11 am 
Newbie

Joined: Mon Jan 15, 2007 3:22 pm
Posts: 6
I have avoided using direct SQL queries with Hibernate, but my guess would be that they don't return typed objects (Users in your case). How can Hibernate know the class for an object returned by some random SQL query?

Try using HQL:

Code:
Session session = .....;
Query query = session.createQuery("from com.hp.ts.bean.User");
List<User> users = (List<User>)query.list();


The users list should contain objects of type User.

Matt


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 17, 2007 2:36 am 
Newbie

Joined: Tue Jan 16, 2007 7:54 am
Posts: 6
Thanks for your repsonse. It helped.

The way I was trying earlier, I found the following way to get the data from that way.

Code:
List userlist = mgr.listUsers();
System.out.println("listing ");
for (int i = 0; i < userlist.size(); i++) {
   Object object[] = (Object[]) userlist.get(i);
   String dbUserId = (String)object[3];
   String dbPassword = (String)object[4];
   System.out.println("User: " + dbUserId +  "Password: " + dbPassword);
   if (userId.equals(dbUserId) && password.equals(dbPassword)) {
       Store user information in session.
      HttpSession session = request.getSession();
      session.setAttribute("id", dbUserId);
      session.setAttribute("userId", dbUserId + " " + dbPassword);
       Return an ActionForward object
      return mapping.findForward("success");
   }
}

But truthfully saying I did not like the way - since there no User Object returned.

I wanted something the way you responded with.
I was trying only("from User") - which was really giving problem
But this ("from ...Package...User") - worked.

Thanks
Vishal


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