1

I have an entity which was generated from a database using JPA tool.

Some of the columns were converted to Object type variable which is presented below.

When I try to use JPA to persist same entity into the database, it gives me an error saying that particular variable is not serialized.

How can I fix this issue?

Entity:

@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

following is the error:

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
Gurkha
  • 1,104
  • 4
  • 20
  • 37
  • 4
    Don't post your interpretation of an error that you don't understand. post the actual exception stack trace, along with the relevant code. – JB Nizet Oct 23 '14 at 15:32
  • @JBNizet i have updated it with the actual exception stack trace. – Gurkha Oct 23 '14 at 15:41
  • Java Object does not implement serializable, the attribute type must implement serializable interface, like String, Date... etc – vzamanillo Oct 23 '14 at 15:45

1 Answers1

2

You do not want to implement Serializable on an entity. Get rid of the serialVersionUID as well.

What are you using for persistence? Are you using Hibernate or clean JPA?

Edit after seeing stack:

You should look into learning what JPA is and how to work with it. http://docs.oracle.com/javaee/6/tutorial/doc/bnbpz.html

And if you would like you should look into Hibernate tutorial as well as it gives you really good methods to persist objects. http://www.mkyong.com/tutorials/hibernate-tutorials/

If you are learning JPA try to play around with Oracle HR scheme (comes with Oracle 11g XE). Here is an example entity of Employee object when I was learning to work with JPA/Hibernate.

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();
    }
}
Dan
  • 106
  • 10
  • 1
    +1 for comment of not using serialVersionUID. According to the Java specification serialVersionUID is implemented to ensure that if you serialize an object to a binary store then retrieve it, if the version has changed since the object was first serialized, then you'll receive back an InvalidClassException. Probably not what you want in your case. Helpful post (http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it) – James Murphy Oct 23 '14 at 15:44
  • @Dan I'm using Eclipselink for persistence. – Gurkha Oct 23 '14 at 15:48