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