-->
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.  [ 4 posts ] 
Author Message
 Post subject: Newbie: need help with fetching a persisted object
PostPosted: Mon Aug 17, 2009 11:17 am 
Newbie

Joined: Mon Aug 17, 2009 10:44 am
Posts: 3
I'm very new to Hibernate.

I have a Persons table.
Persons :
civilID (char)
name (char)
surname (char)
...

what I basically want is to select a Person and store it in HttpSession scope. so I wrote this code :

Code:
      StringBuffer sb = new StringBuffer();
      sb.append(" select");
      sb.append("      p.civilID,");
      sb.append("      p.name,");
      sb.append("      p.surname");
      sb.append("   from");
      sb.append("      Person as p");
      sb.append("   where");
      sb.append("      p.civilID = :civilID");

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Person loggedInUser = (Person) session.createQuery(sb.toString()).setString("civilID", "12345").uniqueResult();
session.getTransaction().commit();
HttpSession httpSession = request.getSession();
httpSession.setAttribute("loggedInUser", loggedInUser);


1) Table 'Persons' has many more columns but I only need/want to fetch 3 of them. so I used a Query instead of load()/get(), right ? (As far as I've read, there's no way to specify the desired columns when doing load()/get() (?))
2) I always get a java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to myapp.domain.Person
exception. I've read about GoF Visitor Pattern and about proxies in the documentation, but couldn't make this query to work. Hibernate executes the correct SQL, but afterwards I always get the ClassCastException.

Also, I tried :
Code:
Person loggedInUser = (Person) session.createQuery(sb.toString()).setString("civilID", "12345").uniqueResult();
Hibernate.initialize(loggedInUser);
session.getTransaction().commit();


but the result is the same.

I tried to get() the whole row and it worked, but it fetched all columns, but I only need three.

here's the hbm.xml file of the Person class :
Code:
  <class name="Person" table="persons">
     <id name="civilID"/>
     <property name="name"/>
     <property name="surname"/>
     <... many more columns here...>
</class>


and finally the Hibernate config file :
Code:
    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/ybs</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <mapping resource="myapp/domain/Person.hbm.xml"/>
    </session-factory>


I need help with this, so please help !


Top
 Profile  
 
 Post subject: Re: Newbie: need help with fetching a persisted object
PostPosted: Mon Aug 17, 2009 5:56 pm 
Newbie

Joined: Mon Aug 17, 2009 10:44 am
Posts: 3
I debugged it and found out that

Code:
session.createQuery(sb.toString()).setString("civilID", "12345").uniqueResult();


returns an Object Array. the method "list()" returns the same Object array as the first element of a List.

In the Hibernate Reference Documentation there is an example like :

Code:
Cat mother = (Cat) session.createQuery(
    "select cat.mother from Cat as cat where cat = ?")
    .setEntity(0, izi)
    .uniqueResult();


Why do I get an Object Array instead of an instance of the Person class ?
I'm stuck here please help.


Top
 Profile  
 
 Post subject: Re: Newbie: need help with fetching a persisted object
PostPosted: Mon Aug 17, 2009 6:56 pm 
Newbie

Joined: Mon Aug 17, 2009 10:44 am
Posts: 3
ok, I've read about it, it's supposed to return an Object array. my fault, but as I said, I'm totally new to Hibernate.

I rephrase my question.

How can I limit the number of columns returned from a query in Hibernate ?
I don't want to retrieve the entire row, just a few columns of it.

(I also tried SQL queries in Hibernate but without any luck.
I get a exception like this : "java.sql.SQLException: Column 'motherName0_0_' not found." where motherName would be the 'next' field in the Person class. and motherName is not listed in the sql statement.)


Top
 Profile  
 
 Post subject: Re: Newbie: need help with fetching a persisted object
PostPosted: Tue Aug 18, 2009 9:20 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
You can do something like this:

"select new User(u.userName, u.password) from User u")

Just remember that the User class will need the appropriate constructor, User(String n, String p)



Code:
        Query query = session.createQuery("select new User(u.userName, u.password) from User u"); 
        List<User> users; 
        users = query.list(); 

        for (User user : users) { 
            System.out.println("User Id:"+user.getFirstName()); 
            System.out.println("User:"+user.getUserName()); 
            System.out.println("User Id:"+user.getUserName()); 
        } 


http://www.coderanch.com/t/455337/Objec ... butes-from

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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