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. 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 timestamp 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
Hibernate
@CollectionOfElementscan 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).