Hello,
I'm new to Hibernate, having currently completed the basics tutorial and currently converting a classic JDBC project to Hibernate.
There is something that I'm trying to do but unfortunately I can't make it work. I hope that anyone can point me to the right direction - here is my issue :
I have a database table called MEDIA, which contains a BLOB column, and many varchar/integer/bigint/timestamp columns. In some situations I need to display a media, in which case I just need to fetch the BLOB, the filename,fileextension,filesize and mimetype. In other situations (for example when using finders) I need to fetch everything besides the BLOB.
During the previous version of the project (classic JDBC), I had a POJO of class "MediaContent", which had attributes like a byte[], the filename,fileextension,filesize and mimetype. I also had a POJO of class "Media", which extends "MediaContent", and had all the other attributes (height,width,title,group,createtime and many others). When displaying a media, I was entirely "filling" a MediaContent object, and in the other cases I was "filling" a Media object, keeping it's byte[] null. The goal of this was just to try to optimize the application, by avoiding to fetch unnecessary data at each retrieval.
So I mapped MediaContent with a MediaContent.hbm.xml file and Media with a Media.hbm.xml file.
Here is the problem : when I now want to display a media, I'm using a query like "from MediaContent where mediaId = :mediaId...." and call the uniqueResult() method on the Query. It was working perfectly in the beginning, when only MediaContent was mapped. But now that Media is also mapped (to the same database table ofcourse), the queries bring me back 2 results each time (thus giving a NonUniqueResultException). So to find out what was happening (and since my query is using the primary key of the table, there is supposed to always be 0 or 1 result), I used the hbQuery.list() method instead and found out that it contains one MediaContent object (perfect ! what I need !) as well as one Media object (not what I need in this case!). Those two objects have the same ID and the same values for the common fields : same record but represented by both mappings.
I thought that by using "from MediaContent" in the query I would get a MediaContent object back but I was very wrong :)
I checked out Hibernate's inheritance strategies but I don't think that this is what I need. I also tried to always use a Media object and forget about the MediaContent object, and to put lazy attributes on some of the fields, but they are always filled even when they are not used (no getter calls).
Can anyone point me to the right direction regarding this ? I can provide my mappings or some code if necessary.
Also, when I will need to retrieve a Media object and fill all the attributes besides the BLOB, what Hibernate mechanism will I have to use ?
During the classic-JDBC days, I could simply make two different queries and use the correct setters for each situation, but now I can't do it like this. I'm pretty sure that this is very easy to do, but I just can't find out what Hibernate mechanism/property/trick I'm supposed to use to accomplish this task.
Thanks for any info ! I can clarify my explanations if needed :)
Nils
*Edit* : note that always using the Media object (no more MediaContent), and sometimes filling fields ABC, sometimes filling fields XYZ, would make me happy. My goal is just to fetch only the fields that I need for each situation. Also, the BLOB is a non-null field, I hope that it's possible to have it being null in a Java object mapped to the table, though.
|