*
The moose likes Object Relational Mapping and the fly likes weird behavior Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "weird behavior" Watch "weird behavior" New topic
Author

weird behavior

Mohit Sinha
Ranch Hand

Joined: Nov 29, 2004
Posts: 125
Hi All,

I am experiencing a weird behavior with Hibernate. Let me know your thoughts on this one as I may be missing the details on some behind the scenes Hibernate magic.
I have a table something like this

table test
-----------------------------------------------------
id version samId val1 val2 msg1 msg2, other cols i will skip for brevity

(id,version,samId,val1,val2) are a composite PK.

In one application scenario I get only the id and version. So I have a HQL (tried with Criteria and SQLQuery using Hibernate entity too) something as simple as
------------
select t from test t where t.id=:id and t.version=:version
------------

When i take the Hibernate generated query and fire it in Oracle IDE I get values like
----------------------------------------------------------
1 v1 11 100 500 testMsg1 testMsg2
1 v1 11 501 1000 tmsg1 tmsg2
1 v1 11 1001 2000 tmsg tmsg2
2 v1 12 10 50 testMsg1 testMsg2
2 v1 12 51 100 tmsg1 tmsg2
2 v1 12 101 200 tmsg tmsg2

but when i get the results through Hibernate (i am printing immediately after fetch) they are something like this. The row count is right but the values are messed up
----------------------------------------------------------
1 v1 11 100 500 testMsg1 testMsg2
1 v1 11 100 500 tmsg1 tmsg2
1 v1 11 100 500 tmsg tmsg2
2 v1 12 10 50 testMsg1 testMsg2
2 v1 12 10 50 tmsg1 tmsg2
2 v1 12 10 50 tmsg tmsg2

It just gives me 1 combination. The Hibernate POJO's and xml files for the test table are straight forward as generated by the tool and have not been modified.
The surprising part is the query which Hibernate used under the covers actually yielded a different result when tried in an Oracle editor.

Do let me know if there is something I am doing wrong or look into some setting in the test pojo and xml file.
I have tried this type of query and even complex queries and they have worked very well but this behavior baffled us and the temporary fix we are employing is to use raw sql and fetch the results as object array and create the hibernate pojo object populated out of it.


Let me know your viewpoints on the same.


Thanks,











Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Your key definition is probably wrong. It looks like Hibernate can't distinguish between the distinct results. Can you show us how you map your composite key?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

Mohit,
Please use a meaningful subject line while posting questions here.
Mohit Sinha
Ranch Hand

Joined: Nov 29, 2004
Posts: 125
Hi Paul you are absolutely correct.
Someone had changed the composite keys in the database and hence the confusion.

Many Thanks - Mohit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: weird behavior
 
Similar Threads
Help need in query
what is the right way to load this object graph. real world problem
A Beginner's Program - Comments?
Cannot Compile Class error on Tomcat 6.0
Need Help With Dynamic text box in JSP