【问题标题】:JPA, When exactly must Entity implement SerializableJPA,实体何时必须实现可序列化
【发布时间】:2014-05-09 04:24:19
【问题描述】:

我目前正在学习 JPA。并且在文档中,它指出只有当实体被其他JVM远程分离时,它才需要可序列化。

但是,出于测试目的,我将实体创建为持久性类(CDI)的内部私有类。 当我尝试使用 EntityManager 持久化实体时。我得到如下异常:

Caused by: Exception [EclipseLink-7155] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The type [class minh.ea.common.Ultilities.DatabaseLogger] for the attribute [this$0] on the entity class [class minh.ea.common.Ultilities.DatabaseLogger$LogRecord] is not a valid type for a serialized mapping. The attribute type must implement the Serializable interface.

据我了解,此异常意味着我的实体属性需要可序列化以及类。那么是什么原因呢,传到哪里去了?

所有这些都在 GlassFish 4.0 Container 下运行。使用 EclipseLink 2.1 的 JPA

我的持久化和内部实体的实现如下:

package minh.ea.common.Ultilities;

import java.util.Date;
import javax.enterprise.context.ApplicationScoped;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.transaction.Transactional;

/**
 *
 * @author Minh
 */

@ApplicationScoped
@minh.ea.common.Ultilities.qualifiers.Database
public class DatabaseLogger implements Logger {

    @PersistenceContext
    private EntityManager em;

    private final long MAX_SIZE=2097152;

    public DatabaseLogger(){

    }

    @Override
    @Transactional
    public void info(Object obj) {
        em.persist(new RecordEntry("INFO", new Date(), obj.toString()));
    }

    @Override
    @Transactional
    public void warn(Object obj) {
        em.persist(new RecordEntry("WARN", new Date(), obj.toString()));
    }

    @Override
    @Transactional
    public void error(Object obj) {
        em.persist(new RecordEntry("ERROR", new Date(), obj.toString()));
    }

    @Override
    @Transactional
    public void fatal(Object obj) {
        em.persist(new RecordEntry("FATAL", new Date(), obj.toString()));
    }

    @Entity
    public class LogRecord{

        @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
        private String type;
        @Temporal(TemporalType.TIMESTAMP)
        private Date time;
        private String message;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public Date getTime() {
            return time;
        }

        public void setTime(Date time) {
            this.time = time;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public LogRecord() {
        }

        public LogRecord(String type, Date time, String message) {
            this.type = type;
            this.time = time;
            this.message = message;
        }
    }

}

最好的问候,

【问题讨论】:

    标签: java jpa eclipselink inner-classes


    【解决方案1】:

    根据this reference,有效的Entity 类必须是顶级类,这意味着您的内部类将不起作用。

    尝试将其拉出到它自己的 Entity 类中,因为它会在适当的系统中,然后再次持久化。

    【讨论】:

    • 谢谢,确实如此,但你能帮我理解为什么它会输出关于序列化的错误吗?
    • RecordEntry 类的映射是什么样的?
    • 它提到了序列化,因为您的实体是一个内部类,因此自动具有指向 JPA 强制映射的外部类的 this$0 属性。因为属性不是实体或基本类型,所以它默认使用可序列化映射,因此例外。如果它是可序列化的,它会尝试将外部实例序列化为表中的 lob 字段。
    • 感谢克里斯的精彩解释。
    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多