File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Wrong Values Retrieved through Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Wrong Values Retrieved through Hibernate" Watch "Wrong Values Retrieved through Hibernate" New topic
Author

Wrong Values Retrieved through Hibernate

Lakshmeepathi Ghiree Anandh
Greenhorn

Joined: Sep 04, 2012
Posts: 5
I have a table named 'TEMP_ATTACHMENT' which has about 8 Columns.
'SESSION_ID' and 'FILE_NAME' are composite-ids.
'TempAttachment' is the pojo and 'TempAttachmentId'(sessionId and fileName) is the class generated for the purpose of composite-id.

After 1st Insertion :
---------------------
DB values (SESSION_ID,FILE_NAME)=(123,'A.txt')
After 1st Retrieval :
--------------------
(sessionId,fileName)=(123,'A.txt')

After 2nd Insertion :
----------------------
DB values (SESSION_ID,FILE_NAME)=(123,'A.txt'),(123,'B.txt')
After 2nd Retrieval :
----------------------
(sessionId,fileName)=(123,'B.txt'),(123,'B.txt') instead of (123,'A.txt'),(123,'B.txt') [ i.e B.txt row gets retrieved twice instead of A.txt and B.txt]

What could cause this problem? A problem in Hibernate or the Oracle Database ? FYI : I printed the values immediately after the HQL.

TWO MORE VALUABLE INFORMATION :
1) When I delete the row which has 'B.txt', 'A.txt' also gets deleted!!!
2) When I insert C.txt, C.txt is getting retrieved thrice instead of A.txt and B.txt and C.txt
Retrieval :


class TempAttachment
{
TempAttachmentId id;
}
class TempAttachmentId
{
BigDecimal sessionId;
String fileName;
}


List<TempAttachment> attachList = new ArrayList<TempAttachment>();
attachList = (List<TempAttachment>)session.createQuery("from TempAttachment where id.sessionId =:sessionId").setString("sessionId",sessionId).list();


I thought BigDecimal in TempAttachmentId and setString in HQL for sessionId might have cause the problem.
Either it should have thrown an exception or this same code shouldn't have worked in the other place where I have used which works perfectly.

Please help!!! Thanks in Advance.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Welcome to Java Ranch!!

Please post your hibernate mappings (assuming you are not using annotations) as well as the objects being mapped (you can omit getters and setters)


[How To Ask Questions][Read before you PM me]
Maciej Opala
Ranch Hand

Joined: Jul 18, 2011
Posts: 38
Also overriding hashCode() and equals() might be useful if You store objects with a collection.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Just so you don't get confused your hibernate entities will use your persistence id, you only need to override equals and hashcode if

1. It is a used as a composite primary key class
2. Instances of that class, loaded in different Hibernate Sessions, are in the same Set (or should be compared)

see the documenation

http://docs.jboss.org/hibernate/core/4.0/manual/en-US/html/persistent-classes.html#persistent-classes-equalshashcode

I missed this part when I first read your post

I thought BigDecimal in TempAttachmentId and setString in HQL for sessionId


It may not be your problem, but I wouldn't generally go for a decimal id. And I am not even sure that BigInteger is really needed. I would go with Long. Is your SessionId a decimal?
Lakshmeepathi Ghiree Anandh
Greenhorn

Joined: Sep 04, 2012
Posts: 5
Hey Bill and Maciej,

Thanks for the reply guys. Bill,Thanks for asking me to post the mapping files! I couldn't connect to coderanch from my system,in the company. But before posting,I wanted to check if the mapping was correct.Unfortunately and fortunately,it was wrong.

Composite-id was not SESSION_ID and FILE_NAME. Instead of that,it was SESSION_ID and USER_ID and that created a lot of confusion.

Just to understand Hibernate better :

1) TempAttachmentId which is a composite primary key class which when given without equals() and hashcode(),what would happen?

2) sessionId is a BigDecimal because the mapping was created with the reverse engineering tool and I did not care to change it and it has been reused at quite a few places
and I am in a position where I cannot change to 'Long' right away because of unorganized codes.Thanks for the suggestion,Bill.

3) In HQL, we are comparing a String and a BigDecimal. Why that doesn't throw any exception? Will some other combination ( for example, comparing an Integer and a String ) throw exception? If so,what is happening while comparing String and a BigDecimal?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

1) TempAttachmentId which is a composite primary key class which when given without equals() and hashcode(),what would happen?


Hibernate relies on these methods for cache lookups. Identifier classes are also expected to implement Serializable.

2) sessionId is a BigDecimal because the mapping was created with the reverse engineering tool and I did not care to change it and it has been reused at quite a few places
and I am in a position where I cannot change to 'Long' right away because of unorganized codes.Thanks for the suggestion,Bill.

hmm Ok

3) In HQL, we are comparing a String and a BigDecimal. Why that doesn't throw any exception? Will some other combination ( for example, comparing an Integer and a String ) throw exception? If so,what is happening while comparing String and a BigDecimal?

Sorry I don't understand what you are talking about, you are going to need to explain this a bit more.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrong Values Retrieved through Hibernate