views:

46

answers:

1

I have a field aliases of type java.util.Set in one Entity. This Set doesn't denote any relationship to an Entity.

Q1. How can I store this aliases field through JPA?

Q2. How this field get stored in database? I think that for the database, this field is a multi-valued attribute.

+3  A: 

How can I store this aliases field through JPA?

JPA 1.0 doesn't support collections of basic types so you'll have to either:

  1. Introduce an entity and map it as a @OneToMany ~or~
  2. Get your Set stored in a BLOB (as a Serializable) ~or~
  3. Mark it @Transient and use another getter/setter to store it using a custom string representation (using a seperator) ~or~
  4. Use an extension of your JPA provider supporting collection of basic types (e.g. Hibernate has the @CollectionOfElements annotation).

Solution #1 would be the cleanest portable solution. Solution #2 can lead to some troubles on upgrades. Solution #3 is a ugly workaround for #2. Solution #4 is clean but non portable.

In JPA 2.0, there is the @ElementCollection annotation for this use case (this is of course the ideal solution).

How this field get stored in database

Depending on the chosen implementation, it may be in a BLOB, in a VARCHAR, in another table.

Pascal Thivent
+1 Very precise and to the point answer
Yatendra Goel