-->
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.  [ 2 posts ] 
Author Message
 Post subject: Get Entities from a native query
PostPosted: Wed Jul 02, 2008 12:10 pm 
Newbie

Joined: Wed Jul 02, 2008 12:02 pm
Posts: 1
Hello hibernate community

Short question:
Is it possible to get an array of objects mixed with entities and native results from a native query?

I have do make a select from different entities AND one table which isn't in my hibernate mapping. I know that's possible to get a array of objects with entities from a native query. But I need the selected entities AND the result of an other table as for example String represenation:

objects[0] = Entitie1;
objects[1] = Entitie2;
objects[2] = String; // "value1"
objects[3] = String; // "value2"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 1:08 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
What you're going to see is either an entity returned, or a set of native types returned. I'm not sure what type of Hatian Vodoo you'd have to invoke to generate a mixture.

Native queries just return the basic database mapped objects in an array.

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=08masteringhqlandnamedqueries

Code:
public static void main(String args[]) {
  String sql = "SELECT * FROM USER";
  Session session = HibernateUtil.beginTransaction();
  SQLQuery query = session.createSQLQuery(sql);
  List users = query.list();
  for (int i = 0; i < users.size(); i++) {
    Object[] o = (Object[]) users.get(i);
    System.out.print(((Integer) o[0])); //id
    System.out.print(((String) o[1]));  //email
    System.out.print(((String) o[2]));  //name
    System.out.println(((String) o[3]));//pass
  }
}

_________________
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.  [ 2 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.