views:

537

answers:

1

Hello,

I am using annotations to map a basic collection of Strings to an existing entity like this:

Inside the parent entity class:

@org.hibernate.annotations.CollectionOfElements
@JoinTable (name="GoalToAchieve_entry", joinColumns=@JoinColumn(name="goalToAchieve_id"))
@org.hibernate.annotations.Sort(type = org.hibernate.annotations.SortType.NATURAL)
private SortedSet<String> entries = new TreeSet<String>();

This works fine and I have a 2-column (goalToAchieve_id and element) table resulting from the join. So I was wondering, how can I add a date/time stamp (auto-generated by MySQL) to each String of the collection, so that i can display a 3rd column with a time stamp every time a new String is added to the collection? The objective is to use a collection of simple objects (Strings) and not have to create a whole new entity (with a time/date field).

Is there a recommended way to do that? I believe even if I provide a timstamp field at the jsp interface which handles the adding of new Strings to the collection, it would still be a hibernate issue, since I need the timestamp to be persisted on the db.

Thanks in advance for any help.

Kindest regards

+1  A: 

Hibernate @CollectionOfElements can be used with more than just simple types. You can define a class which contains two fields, your String value, plus a timestamp, and annotate that class with @Embeddable. Hibernate will then persist that to the database as two separate data columns. This class doesn't represent an Entity, just a composite value-type.

The next problem is how do you generate that timestamp. You could do it in java, with the default initialised value of the field being "new Date()" (or whatever).

skaffman
Thanks! I was thinking more along the lines of using, @Temporal Would you think that could work in this case to generate a new timestamp with every new insert of a String value?
denchr
Aye, that should work, I think. I've never actually used @Temporal myself, and its behaviour doesn't seem well documented, but it looks handy.
skaffman