gpt4 book ai didi

java - JPA,实体什么时候必须实现可序列化

转载 作者:搜寻专家 更新时间:2023-11-01 03:23:10 26 4
gpt4 key购买 nike

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

但是,出于测试目的,我将实体创建为持久性类 (CDI) 的内部私有(private)类。当我尝试使用 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 容器下运行。使用 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;
}
}

}

最好的问候,

最佳答案

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

尝试将它拉出到它自己的 Entity 类中,就像在适当的系统中一样,然后再次持久化。

关于java - JPA,实体什么时候必须实现可序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23556586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com