design question: optional relations
mars1412 Mar 5, 2008 5:11 PMI have an entity WebUser that has a OneToOne relationship to ImageMetaInfo which has a OneToOne relationship to ImageData.
WebUser to ImageMetaInfo is OneToOne, because other entities will also have such relations to ImageMetaInfo.
The ImageMetaInfo relation to ImageData is because I want to be able to lazy load the Image Data.
A WebUser is not required to have an image, so webUser.userImageMetaInfo may be null.
Now I am thinking of a nice way to handle this situation.
1) If I just use the entity as is, I would have to use a lot of checks in the gui to make sure, I don't get null-pointer excpetions - like:
<s:div rendered="#{not empty webUser.userImageMetaInfo.imageData.data}"> <s:graphicImage value="#{webUser.userImageMetaInfo.imageData.data}}"/> </s:div>
I don't like this approach: too easy to forget the check.
2) make the userImageMetaInfo required
I could insert the default image, when the webUser is created.
This would mean, I have a lot of copies of the same default images in my database, wasting database-space (and my import scripts would become a horror to write).
3) I thought of using a default image when the userImageMetaInfo is null - but how could I implement that?
3a) do the check in the entities getter
so webUser.getUserImageMetaInfo would create a new ImageMetaInfo including a new ImageData object and return this
the problem here is, this:
- I could assign the new entity to the webUser.userMetaImage immediately - but this would mean, that the default image would be stored when the webUser is updated: I don't want that (see point 2)
- When I do not assign the new entity to the webUser.userMetaImage immediately, how could I detect changes on this new entity?
the gui could just use
<s:fileUpload data="#{webUser.userImageMetaInfo.imageData.data}" />
and would alter the new entity. So I would have to check if the image has been changed before the webUser is persisted.
Should I use some Hibernate events for this: @PreUpdate
3b) use special getters/setters on a EntityHome object?
then I could override update and check if the image is my default image or not (if not, I would persist the new image and set it on the webuser)
any comments, better ideas?