1

I implemented a hibernate query and would like to assign the result to one of my class variables.

The problem is that the results of hibernate queries seem to be objects or something, as the syso of a result looks very strange:

[exercise.java.basics.storage.WarehouseProduct@77f6d2e3]

This is the method executing the query:

 public void updateStock() {

    Session session = getSessionFactory().getCurrentSession();

    Criteria criteriaNail = session.createCriteria( WarehouseProduct.class );
    criteriaNail.add( Restrictions.like( "productName", String.valueOf( Product.NAIL ) ) );
    List nailCountResult = criteriaNail.list();

    System.out.println( nailCountResult.toString() );

    }

The database has only 2 colums and the value I need is in the second. What I would like to do is something like this:

this.nailCount  = nailCountResult.[XYZ --> Get the value from the second column];

Is something like this possible? How can I cast these result objects to something readable?

best regards daZza

2
  • You need to read about Object#toString(). Commented Feb 17, 2014 at 13:33
  • Implement toString() method for WarehouseProduct class Commented Feb 17, 2014 at 13:34

2 Answers 2

3

First of all I suggest to change the line to

 List<WarehouseProduct> nailCountResult = criteriaNail.list();

And now it is not a ResultSet, it's a list of WarehouseProduct Objects.

You can access each object with index.

You can loop over the result list and see them like

 for( WarehouseProduct wp : nailCountResult )   {
   System.out.println( wp.nailCount);    
  }

As a side note, you are breaking encapsulation here. Please look in to it.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, I'll be damned. It works :) Thanks for your help and I'll look into encapsulation later.
0

All you need to to is this

String value=nailCountResult.get(0).getXXXX();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.