Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem in saving List<String> through JPA?

I have already seen

How to persist a property of type List<String> in JPA?

and

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.

like image 333
Yatendra Avatar asked Jan 21 '23 11:01

Yatendra


2 Answers

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

  • https://sourceforge.net/projects/hibernate/files/hibernate3/3.5.5-Final
  • http://www.eclipse.org/eclipselink/downloads/

Reference

  • JPA 2.0 Specification
    • Section 11.1.12 "ElementCollection Annotation"
like image 82
Pascal Thivent Avatar answered Feb 09 '23 06:02

Pascal Thivent


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.

like image 45
James Avatar answered Feb 09 '23 06:02

James