@ElementCollection is a new annotation of JPA 2, and is provided by Hibernate 3.5-beta4. I tried to use this feature in an entity class:
@Entity
public class Customer implements Serializable {
@Id
...
@ElementCollection(fetch=EAGER)
@CollectionTable(name="customer_code", joinColumns=@JoinColumn(name="customer_fk"))
@Column(name="code_fk")
private Set<Integer> codes;
}
However, the concrete type of attribute "codes" is Hibernate's PersistentSet, so that "codes" cannot be updated outside of a transaction, e.g. via checkboxes in JSF based pages. Therefore, I extended the entity class:
@Entity
public class Customer implements Serializable {
@Id
...
@ElementCollection(fetch=EAGER)
@CollectionTable(name="customer_code", joinColumns=@JoinColumn(name="customer_fk"))
@Column(name="code_fk")
private Set<Integer> codesDB;
@Transient
private Set<Integer> codesUI;
}
Now, I can update the transient attribute codesUI in the JSF pages. But, new problems arrived:
a) "codesUI" CANNOT be instantiated via @PostLoad when reading a Customer object from the database - maybe because codesDB is considered as an association. OK, so I extended the get method for codesUI to instantiate it from codesDB.
b) "codesDB" CAN be instantiated by @PrePersist when persisting a new Customer object via EntityManager.persist(). Fine.
c) "codesDB" CANNOT be updated by @PreUpdate when an existing Customer object is updated via EntityManager.merge(). When the @PreUpdate method is invoked the "codesUI" attribute suddenly is null. Thus for updates I have to modify my DAO method, but for new creations everything works fine through @PrePersist...
Any hint or comment is highly appreciated!