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/
@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
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"}
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
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
So design a table structure.
--
Lew
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