I have a class representing a forum post with an optional image :
Code:
public class Post {
...
protected String title;
protected ImageOrFile attachement;
...
}
ImageOrFile is a class that holds the file name, byte[], ...
I'm trying to find the best mapping for this situation, knowing that :
* This is a base class, and I have different subclasses for different forums.
So, I would like to hard code as little table names as possible (I use XDoclet).
* It's quite important to use some kind of lazy loading as the attachement holds a byte[] that can be quite large. Further more, usually, the post title isn't uses in the same thread as the attachement's byte[] that is served by a separate Servlet.
I first thought about using something like
@hibernate.component prefix="attachement_" lazy="true"
The advantage is that I have everything in the same table, so I don't need to specify anything in the subclasses.
But for this to work, I need to run the bytecode instrumentation task. It seems that this bytecode instrumentation is quite a heavy process and the documentation is full of warnings that this it isn't recommended to use that "mostly marketing" feature.
So, the alternative seems to be using a one-to-one relation :
@hibernate.one-to-one constrained="true" outer-join="false" cascade="all"
But this has also some drawbacks too :
The attachement has to be an @hibernate.class, with it's own class and table.
This would mean that for each Post' subclass, I would also have to create an ImageOfFile subclass.
For a single class, this would be ok, but if the goal is to have many forums (so, many Post' subclasses), then it forces to make and maintain a lot of plumbing.
As I use many attachements in my applications, I have this problem quite often, and I would love to get the advises from more experimented Hibernate users.
Thanks you.
Sylvain.