views:

107

answers:

3

I have already seen

http://stackoverflow.com/questions/287201/how-to-persist-a-property-of-type-liststringin-jpa

and

http://stackoverflow.com/questions/796347/map-a-list-of-strings-with-jpa-hibernate-annotations

=============================================================================

I am trying to save

List<String>

through JPA. I came to know that JPA 1.0 doesn't have any way to persist collection of non-entity classes but JPA 2.0 have included @CollectionOfElements annotation.

First, I am not sure which JPA 2.0 implementation to use. Earlier I was using toplink-essentials but it doesn't have @CollectionOfElements annotation.

I downloaded eclipse-link but didn't find any jar files inside it.

So my question is to which JPA 2.0 implementation to use which provides jar files so that I can include those jars in my project as I have done for toplink-essentials.

+3  A: 

First, I am not sure which JPA 2.0 implementation to use. Earlier I was using toplink-essentials but it doesn't have @CollectionOfElements annotation.

TopLink-Essential is not a JPA 2.0 implementation. And as pointed out by James, note that the JPA 2.0 annotation is @ElementCollection, @CollectionOfElements is the Hibernate specific annotation which is now deprecated in favor of the JPA annotation.

I downloaded eclipse-link but didn't find any jar files inside it.

What did you download? The EclipseLink 2.1.1 Installer Zip (28 MB) that you can get from the download page contains an all-in-one jar in the jlib directory.

So my question is to which JPA 2.0 implementation to use which provides jar files so that I can include those jars in my project as I have done for toplink-essentials.

Any of them. I would personally use Hibernate 3.5.x or EcliseLink 2.x that you can get respectively from

Reference

  • JPA 2.0 Specification
    • Section 11.1.12 "ElementCollection Annotation"
Pascal Thivent
+2  A: 

Note that JPA 2.0 defines @ElementCollection not @CollectionOfElements. It can be used to store List. @CollectionOfElements is a Hibernate extension.

In TopLink Essentials (JPA 1.0) you could still map this, but would need to use a DescriptorCustomizer to define a DirectCollectionMapping in code.

In EclipseLink 1.0, this was defined as a @BasicCollection, and in JPA 2.0 and EclipseLink >= 1.2 this was defined as @ElemenetCollection.

James
Good catch, didn't pay attention to the fact that the OP was mentioning the Hibernate specific annotation.
Pascal Thivent
A: 

Just use ArrayList instead of List. I tryed

@Entity
public class Orar implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private ArrayList<String> lectii;

and it works ;)

Andrei
Your ArrayList will get serialized in a BLOB, this is totally different.
Pascal Thivent