我有一个使用JPA工具从数据库生成的实体。
某些列已转换为Object类型变量,如下所示。
当我尝试使用JPA将同一个实体持久存储到数据库中时,它会给出一个错误,指出特定变量未被序列化。
如何解决此问题?
实体:
@Entity
@Table(name = "EPRECERT")
public class TestEprecert implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "AUTHORIZATION_NUM")
private String authorizationNum;
@Column(name="BODY_AREA_OTHER")
private Object bodyAreaOther;
//more fields, getters and setters
以下是错误:
Caused by: Exception [EclipseLink-7155] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The type [class java.lang.Object] for the attribute [bodyAreaOther] on the entity class [class com.ceiwc.es.test.model.TestEprecert] is not a valid type for a serialized mapping. The attribute type must implement the Serializable interface.
at org.eclipse.persistence.exceptions.ValidationException.invalidTypeForSerializedAttribute(ValidationException.java:1139)
at org.eclipse.persistence.internal.jpa.metadata.converters.SerializedMetadata.process(SerializedMetadata.java:99)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor.processSerialized(MappingAccessor.java:1948)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor.processMappingConverter(MappingAccessor.java:1775)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor.processMappingValueConverter(MappingAccessor.java:1796)
at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.BasicAccessor.process(BasicAccessor.java:419)
at org.eclipse.persistence.internal.jpa.metadata.MetadataDescriptor.processMappingAccessors(MetadataDescriptor.java:1536)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor.processMappingAccessors(ClassAccessor.java:1648)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processMappingAccessors(EntityAccessor.java:1234)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.process(EntityAccessor.java:697)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage2(MetadataProject.java:1793)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:576)
at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:585)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1869)
... 45 more
答案 0 :(得分:2)
您不希望在实体上实现Serializable。摆脱serialVersionUID。
你有什么用于坚持?你在使用Hibernate还是清理JPA?
看到堆栈后编辑:
您应该了解JPA是什么以及如何使用它。 http://docs.oracle.com/javaee/6/tutorial/doc/bnbpz.html
如果您愿意,您应该查看Hibernate教程以及它为您提供非常好的方法来持久化对象。 http://www.mkyong.com/tutorials/hibernate-tutorials/
如果您正在学习JPA,请尝试使用Oracle HR方案(Oracle 11g XE附带)。当我学习使用JPA / Hibernate时,这是一个Employee对象的示例实体。
package entities;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.validator.NotNull;
@NamedQueries({
@NamedQuery(name=Employee.QUERY_ALL_BY_MANAGER_ID, query="select e from Employee e where e.managerId=:" + Employee.PARAM_MANAGER_ID),
@NamedQuery(name=Employee.QUERY_ALL, query="select e from Employee e order by e.department.departmentName asc")
})
@Entity
@Table(name = "EMPLOYEES")
public class Employee {
public static final String QUERY_ALL = "employee.getAllEmployees";
public static final String QUERY_ALL_BY_MANAGER_ID = "employee.getEmployeesUnderSpecificManager";
public static final String PARAM_MANAGER_ID = "managerId";
@OneToMany(targetEntity = Department.class, mappedBy = "managerId", cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List<Department> departments;
@Id
@Column(name = "EMPLOYEE_ID")
private Integer employeeId;
@Column(name = "FIRST_NAME")
private String firstName;
@NotNull
@Column(name = "LAST_NAME")
private String lastName;
@NotNull
@Column(name = "EMAIL")
private String email;
@Column(name = "PHONE_NUMBER")
private String phoneNumber;
@NotNull
@Column(name = "HIRE_DATE")
private Date hireDate;
@NotNull
@ManyToOne(targetEntity = Job.class, fetch = FetchType.EAGER)
@JoinColumn(name = "JOB_ID")
private Job job;
@Column(name = "SALARY")
private Integer salary;
@Column(name = "COMMISSION_PCT")
private Integer commissionPct;
@Column(name = "MANAGER_ID")
private Integer managerId;
@ManyToOne(targetEntity = Department.class, fetch = FetchType.EAGER)
@JoinColumn(name = "DEPARTMENT_ID")
private Department department;
@OneToMany(targetEntity = Employee.class, mappedBy = "managerId", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Employee> team;
public Employee() {
}
public List<Employee> getTeam() {
return team;
}
public List<Department> getDepartments() {
return departments;
}
public void setDepartments(List<Department> departments) {
this.departments = departments;
}
public Integer getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public Job getJob() {
return job;
}
public void setJob(Job job) {
this.job = job;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
public Integer getCommissionPct() {
return commissionPct;
}
public void setCommissionPct(Integer commissionPct) {
this.commissionPct = commissionPct;
}
public Integer getManagerId() {
return managerId;
}
public void setManagerId(Integer managerId) {
this.managerId = managerId;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return getFirstName() + " " + getLastName();
}
}