Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

1,151 views
Skip to first unread message

Andrea Francia

unread,
Nov 13, 2008, 10:40:42 AM11/13/08
to
What is the smartest way to get an entity with a field of type
List<String> get persisted?

Command.java
---------------------------------------------------
package persistlistofstring;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Persistence;

@Entity
public class Command implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Basic
List<String> arguments = new ArrayList<String>();

public static void main(String[] args) {
Command command = new Command();

EntityManager em = Persistence
.createEntityManagerFactory("pu")
.createEntityManager();
em.getTransaction().begin();
em.persist(command);
em.getTransaction().commit();
em.close();

System.out.println("Persisted with id=" + command.id);
}
}
-------------------------------------------------------

This code produces:
> Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named pu: Provider named oracle.toplink.essentials.PersistenceProvider threw unexpected exception at create EntityManagerFactory:
> oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
> Local Exception Stack:
> Exception [TOPLINK-30005] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException
> Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@11b86e7
> Internal Exception: javax.persistence.PersistenceException: Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.EntityManagerSetupException
> Exception Description: predeploy for PersistenceUnit [pu] failed.
> Internal Exception: Exception [TOPLINK-7155] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.ValidationException
> Exception Description: The type [interface java.util.List] for the attribute [arguments] on the entity class [class persistlistofstring.Command] is not a valid type for a serialized mapping. The attribute type must implement the Serializable interface.
> at oracle.toplink.essentials.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:143)
> at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createEntityManagerFactory(EntityManagerFactoryProvider.java:169)
> at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:110)
> at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
> at persistlistofstring.Command.main(Command.java:30)
> Caused by:
> ...

--
Andrea Francia
FtpServerStub ease your FTP Client unit-testing:
http://andreafrancia.it/ftpserverstub/

Lew

unread,
Nov 13, 2008, 11:38:25 AM11/13/08
to
On Nov 13, 10:40 am, Andrea Francia
<andrea.fran...@gmx.REMOVE_FROM_HERE_ohohohioquestoèdatogliereohohoho_TO_HERE.it>
wrote:

> What is the smartest way to get an entity with a field of type
> List<String> get persisted?

@OneToMany

In a normalized database, the List will correspond to a set of rows
from a related table.

It's a little unusual to have a table with only a single String
column, though. Usually there's a somewhat more complex structure.
What is the corresponding table structure?

--
Lew

Andrea Francia

unread,
Nov 13, 2008, 12:19:14 PM11/13/08
to
On Nov 13, 10:40 am, Andrea Francia wrote:
>> What is the smartest way to get an entity with a field of type
>> List<String> get persisted?


Lew wrote:
> It's a little unusual to have a table with only a single String
> column, though. Usually there's a somewhat more complex structure.
> What is the corresponding table structure?

I want represents command line arguments of a command.
The complete data structure should be something like that:

Class data structure:
---------------------
public class Command {
private String workindDir; // where start the command
private List<String> commandLineArguments; // command line arguments

...
}

Instance example:
-----------------
workingDir = "/var/log"
commandLineArguments = list of {"/bin/ls", "-l", "--color=always"}

Lew

unread,
Nov 13, 2008, 1:58:44 PM11/13/08
to
Lew wrote:
>> It's a little unusual to have a table with only a single String
>> column, though.  Usually there's a somewhat more complex structure.
>> What is the corresponding table structure?

Andrea Francia wrote:
> I want represents command line arguments of a command.
> The complete data structure should be something like that:
>
> Class data structure:
> ---------------------
> public class Command {
>    private String workindDir; // where start the command
>    private List<String> commandLineArguments; // command line arguments
>    ...
> }
>
> Instance example:
> -----------------
> workingDir = "/var/log"
> commandLineArguments = list of {"/bin/ls", "-l", "--color=always"}

That's not a table structure.

What's the corresponding table structure?

JPA is all about object-to-relational mapping (ORM). You haven't told
us the relational structure.

--
Lew

Andrea Francia

unread,
Nov 13, 2008, 3:02:37 PM11/13/08
to
Lew wrote:
> That's not a table structure.

Sorry, I realize that I did not understand your question.
There is no a pre-existing table structure.

My problem is not the mapping a new object model to an existing table
structure. I need only persist a given object model.

--
Andrea Francia
http://andreafrancia.blogspot.com/2008/07/colinux-linux-dentro-windows.html

Lew

unread,
Nov 13, 2008, 3:51:48 PM11/13/08
to
On Nov 13, 3:02 pm, Andrea Francia <andrea.fran...@REMOVE-FROM-

HERE.ohoihihoihoih.TO-HERE.gmx.it> wrote:
> Lew wrote:
> > That's not a table structure.
>
> Sorry, I realize that I did not understand your question.
> There is no a pre-existing table structure.
>
> My problem is not the mapping a new object model to an existing table
> structure. I need only persist a given object model.

So design a table structure.

--
Lew

Lew

unread,
Nov 13, 2008, 5:03:28 PM11/13/08
to
On Nov 13, 10:40 am, Andrea Francia
<andrea.fran...@gmx.REMOVE_FROM_HERE_ohohohioquestoèdatogliereohohoho_TO_HERE.it>
wrote:

> What is the smartest way to get an entity with a field of type
> List<String> get persisted?
>
> Command.java
> ---------------------------------------------------
> package persistlistofstring;
>
> import java.io.Serializable;
> import java.util.ArrayList;
> import java.util.List;
> import javax.persistence.Basic;
> import javax.persistence.Entity;
> import javax.persistence.EntityManager;
> import javax.persistence.GeneratedValue;
> import javax.persistence.GenerationType;
> import javax.persistence.Id;
> import javax.persistence.Persistence;
>
> @Entity
> public class Command implements Serializable {
>
>      @Id
>      @GeneratedValue(strategy = GenerationType.AUTO)
>      Long id;
>      @Basic
>      List<String> arguments = new ArrayList<String>();

You might have some luck if you declare 'arguments' as 'ArrayList'
instead of 'List', since 'ArrayList' implements 'Serializable'.

You should still have a table design, though.

Read this chapter:
<http://java.sun.com/javaee/5/docs/tutorial/doc/bnbpy.html>

It's what inspired me to suggest 'ArrayList'.

Also, different JPA implementations (TopLink, Hibernate, OpenJPA)
extend the JPA standard in different ways, sometimes allowing things
not strictly permitted by vanilla JPA.

--
Lew

0 new messages