获取字段类型为List的实体最聪明的方法是什么?

命令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);
    }
}

该代码生成:

> 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(命令JAVA:30)
> Caused by: 
> ...

推荐答案

使用一些JPA2实现:它添加了一个@ElementCollection注释,类似于Hibernate注释,完全符合您的需要.有一个例子是here.

编辑

正如下面的 comments 所提到的,正确的JPA2实现是

javax.persistence.ElementCollection

@ElementCollection
Map<Key, Value> collection;

见:http://docs.oracle.com/javaee/6/api/javax/persistence/ElementCollection.html

Java相关问答推荐

如何在Java中使用Rest模板通过查询参数发送GET请求

Informix PrepareStatement引发错误-将LIMIT Clause添加到查询时,字符到数字的转换过程失败

在Spring Boot中测试时出现SQL语法错误

如何用javac编译Java类,即使对像java.lang.*这样的基本类也没有任何依赖关系?

如何为具有多对多关系的实体的给定SQL查询构建JPA规范?

为什么如果数组列表中有重复项,我的代码SOMETIMES不返回true?

如何在Android上获取来电信息

取消按钮,但没有任何操作方法引发和异常

Junit with Mockito for java

Exe4j创建的应用程序无法再固定在任务栏Windows 11上

编译多个.Java文件并运行一个依赖于用户参数的文件

Java LocalTime.parse在本地PC上的Spring Boot中工作,但在Docker容器中不工作

为什么S的文档中说常量方法句柄不能在类的常量池中表示?

获取所有可以处理Invent.ACTION_MEDIA_BUTTON Android 13 API33的Android包

TinyDB问题,无法解析符号';上下文&

无法在Java中获取ElastiCache的AWS CloudWatch指标

控制器建议异常处理

让标签占用JavaFX中HBox的所有可用空间

如果c不为null,Arrays.sort(T[]a,Comparator<;?super T>;c)是否会引发ClassCastException?

MapStruct记录到记录的映射不起作用