User development,
A new message was posted in the thread "Weird usage of @ElementCollection":
http://community.jboss.org/message/524306#524306
Author : Juergen Zimmermann
Profile :
http://community.jboss.org/people/Juergen.Zimmermann
Message:
--------------------------------------------------------------
@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",
http://community.jboss.org/mailto:joinColumns=@JoinColumn(name="...)
@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",
http://community.jboss.org/mailto:joinColumns=@JoinColumn(name="...)
@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!
--------------------------------------------------------------
To reply to this message visit the message page:
http://community.jboss.org/message/524306#524306