Results 1 to 13 of 13
  1. #1
    nareshthota is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Problems with Session.createQuery...

    I have a requirement to retrieve the data from the table in the form of list...iam using following query...session.createQuery("from in.abc.Customer").list(); with this query no problem...but if i use session.createQuery("select id,name from in.abc.customer") and if my pojo contains other fields like email(more than fields which is used in the query)...it is throwing exception that invalid column id "email"....

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Problems with Session.createQuery...

    How are you executing that query?
    It should return an Object[] containing the id and name, not a Customer.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    nareshthota is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Problems with Session.createQuery...

    consider my code....
    public List getAll(){

    Session session=sessionFactory.openSession();
    Query query= (Query) session.createQuery("SELECT id,c_name,vertical_id FROM com.abc.clients.domain.Client");

    List newlist=query.list();

    Iterator iterator=newlist.iterator();
    while (iterator.hasNext()) {

    Client client = (Client) iterator.next();
    String vid=client.getVertical_id();
    list=session.createQuery("FROM com.abc.clients.domain.ClientSetting where id="+vid).list();
    Iterator it=list.iterator();
    while(it.hasNext()){
    ClientSetting csetting=(ClientSetting) it.next();
    String name=csetting.getName();
    client.setVertical_id(name);
    }

    }



    return newlist;


    it is throwing an exception Ljava.lang.Object cannot be cast to com.abc.clients.domain.Client....and the line which cause exception is Client client = (Client) iterator.next();....please provide a solution for this...iam struggling it for one day....

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Problems with Session.createQuery...

    The reason is that the List is not a List<Client>.
    It is a List<Object[]>, where each entry in the Object[] maps to one of the fields in the SELECT statement.
    So in your case:
    object[0] -> id
    object[1] -> c_name
    object[2] -> vertical_id.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    nareshthota is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Problems with Session.createQuery...

    so do i need to use List<Client> insted of List....i tried wit h so many alternatives like i used Object[] directly and Client[] etc.,,,...but it is throwing same exception...
    Last edited by nareshthota; 03-16-2012 at 11:30 AM.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Problems with Session.createQuery...

    Object[] result = (Object[]) iterator.next();
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    nareshthota is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Problems with Session.createQuery...

    sorry for asking...first of all how to get Object[] from query...it does not contain any method which returns results as object[]....

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Problems with Session.createQuery...

    That's the list.
    As soon as you put specific fields into the SELECT clause Hibernate's list() method will return a List<Object[]> rather than a List<mapping class>.
    So your iterator is iterating over a List<Object[]>.
    And iterator.next() is returning an Object[] and not a Client.

    Now try it in your code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    nareshthota is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Problems with Session.createQuery...

    Then my further code is giving problem...how can I change this vertical_id i.e, result[2] with another value....

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Problems with Session.createQuery...

    Show us your code, because I can't see your computer from here...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Problems with Session.createQuery...

    Oh, and please use [code] tags [/code].
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    nareshthota is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Problems with Session.createQuery...

    okay....problem resolved....thanx for responding....

  13. #13
    nareshthota is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Problems with Session.createQuery...

    Can we have multiple pojos for a single table....Is it recommended....

Similar Threads

  1. NullPointerException from createQuery()
    By ribbs2521 in forum New To Java
    Replies: 4
    Last Post: 10-23-2009, 01:40 PM
  2. Replies: 1
    Last Post: 04-21-2009, 11:20 PM
  3. Replies: 2
    Last Post: 12-23-2008, 06:35 PM
  4. problems with applet session between to two applets
    By elizabeth in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 03:23 PM
  5. Replies: 1
    Last Post: 08-06-2007, 11:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •