5

Hibernate - Query returns null for all fields in Entity

Long fooId = 39;    
Query query = getCurrentSession().createQuery("from FooEntity where deleted IS FALSE AND id=:fooId" );
query.setParameter( "fooId", fooId );
FooEntity fooEntity = ( FooEntity ) query.uniqueResult();

Inspecting the FooEntity the following result is shown

(id=null, name=null, deleted=null)

While the same query returns perfect result from db

select * from foo where deleted IS FALSE AND id=39
(id, name, deleted) => (39, 'Bar', false)

It has to be noted that this occurs in random cases only. Most of the time hibernate returns perfect result.

2 Answers 2

3

Please try to log or sysout the value in the next line.

For me and the OP the issue was same, just Java Debugger was not showing the value. The value was there all the time.

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

2 Comments

Is there any way to resolve this? Is it a bug into debugger or Hibernate-related?
Not that I know of. This issue isn't very consistent. I have debugged the same code many times over since the issue, and it never occurred again. I'd suggest you kill the IDE and the associated JVM threads and try again, since that's the only thing changed between multiple debug sessions for me. Also please comment back if you find this issue consistent across JVM processes somehow.
2

might be a concurrent problem (when we've had same behavior we noticed a shared session between threads)

Shared entity or session between threads or shared entity between sessions could cause this kind of issue

2 Comments

@Cache( usage = CacheConcurrencyStrategy.READ_WRITE ) this annotation on Entity has any effect?
it means you also use secondary cache, right? what kind of cache? yes, READ_WRITE is the safest but it's only the concurrency strategy, in case of using secondary cache you also have to think about the transactional strategy if it is a distributed cache and the transaction isolation, e.g. you've just created the item and it cannot be seen at that point from another, etc etc ... There are lot of places where things can go wrong, you specified just a piece of information

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.