【发布时间】: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