我目前正在学习JPA。在文档中,它指出只有当实体被其他JVM远程分离时,它才需要是Serializable。
但是,出于测试目的,我创建了我的实体作为我的持久化类(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.
我理解这个例外,意味着我的实体属性需要是Serializable以及Class。那是什么原因,它被传递到哪里?
所有这些都在GlassFish 4.0 Container下运行。 JPA使用EclipseLink 2.1
我对持久性和内部实体的实现如下:
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;
}
}
}
致以最诚挚的问候,
答案 0 :(得分:1)
根据this reference,有效的Entity
类必须是顶级类,这意味着您的内部类将无法工作。
尝试将其拉出到自己的Entity
类中,就像它在适当的系统中一样,然后再次存在。