0
votes

I've developed a simple code that displayes employee name by using Jpa's one of CRUD operations(find) on entity classes "Employee"& "Department" it worked properly while running the code , but the real problem came when I created a jar file from the application, an exception appeared from the jar file , I wrote the exception in a txt file

Here is the Employee class

    package com.tutorialspoint.eclipselink.entity;

    import java.util.*;
    import javax.persistence.*;

    @Entity
    public class Employee {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)  
    private int eid;

    @Temporal(TemporalType.TIMESTAMP)
    private java.util.Date dop;

    private String ename;
    private double salary;
    private String deg;

    @OneToOne(targetEntity = Department.class)
    private Department dept;

    @OneToMany (targetEntity = Staff.class)
    private ArrayList<Staff> staffs;

    public Employee(int eid, String ename, double salary, String deg) {
       super( );
       this.eid = eid;
       this.ename = ename;
       this.salary = salary;
       this.deg = deg;
    }

    public Employee( ) {
         super();
    }

    public Date getDop() {
         return dop;
    }

    public void setDop(Date dop) {
         this.dop = dop;
    }

    public int getEid( ) {
        return eid;
    }

    public void setEid(int eid) {
        this.eid = eid;
    }

    public Department getDept() {
        return dept;
    }

    public void setDept(Department dept) {
        this.dept = dept;
    } 

    public String getEname( ) {
        return ename;
    }

    public void setEname(String ename) {
       this.ename = ename;
    }

    public double getSalary( ) {
       return salary;
    }

    public void setSalary(double salary) {
       this.salary = salary;
    }

    public String getDeg( ) {
         return deg;
    }

    public void setDeg(String deg) {
        this.deg = deg;
    }

    public ArrayList<Staff> getStaffs() {
        return staffs;
    }

    public void setStaffs(ArrayList<Staff> staffs) {
        this.staffs = staffs;
    }
    }

and here is the class that displays employee name and degree

    public void findEmployee(){
    try{
        EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "Eclipselink_JPA" );
        EntityManager entitymanager = emfactory.createEntityManager();
        Employee employee = entitymanager.find( Employee.class, 204 ); 
        JOptionPane.showMessageDialog(null, employee.getEname()+ 
        "=>"+employee.getDeg());

        }catch(Exception ex){
              JOptionPane.showMessageDialog(null,ex.getMessage());
              displayMsg(ex.getMessage());
         }
        }
    public void displayMsg(String msg){
    // i made this method to display the exception in a txt file
        File f = new File("E:\\bug2.txt");
        FileWriter fw = new FileWriter(f);
        PrintWriter pw = new PrintWriter(fw);
        pw.println(msg);
        pw.flush();pw.close();

    }

and here is the exception " Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException Exception Description: Deployment of PersistenceUnit [Eclipselink_JPA] failed. Close all factories for this PersistenceUnit. Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.IntegrityException

Descriptor Exceptions:

Exception [EclipseLink-1] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException

Exception Description: The attribute [teacherSet] is not declared as type ValueHolderInterface, but its mapping uses indirection.

Mapping: org.eclipse.persistence.mappings.ManyToManyMapping[teacherSet] Descriptor: RelationalDescriptor(com.tutorialspoint.eclipselink.entity.Clas --> [DatabaseTable(CLAS)])

Exception [EclipseLink-1] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException Exception Description: The attribute [clasSet] is not declared as type ValueHolderInterface, but its mapping uses indirection. Mapping: org.eclipse.persistence.mappings.ManyToManyMapping[clasSet] Descriptor: RelationalDescriptor(com.tutorialspoint.eclipselink.entity.Teacher --> [DatabaseTable(TEACHER)])

Exception [EclipseLink-1] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException

Exception Description: The attribute [staffs] is not declared as type ValueHolderInterface, but its mapping uses indirection. Mapping: org.eclipse.persistence.mappings.ManyToManyMapping[staffs]

Descriptor: RelationalDescriptor(com.tutorialspoint.eclipselink.entity.Employee --> [DatabaseTable(EMPLOYEE)])

Runtime Exceptions: --------------------------------------------------------- "

so what can be done?? knowing that the program works well when running the code from IDE but this exception happens when i built it and created jar file and ran the jar file

2

2 Answers

0
votes

Exceptions that involve interfaces like ValueHolders and indirection is most likely a case of problems due to entity weaving.

Entity weaving is a process of modifying the compiled entities' bytecode so that they implement more interfaces and add new methods such that they can handle things like indirection and lazy-loading, among other features.

Is your IDE Oracle JDeveloper? It is one of the IDEs that, by default, have a run configuration that does this automatically, so that your entities work correctly. This can be configured in other IDEs in a similar manner - by adding -javaagent:<path to eclipselink JAR> as a program argument (or Java Option in some IDEs). Check this blog post for some quick info.

It might the be case in your deployment that Eclipselink's dynamic (runtime) weaving has failed (or is incomplete for some reason). Perhaps you should consider static weaving before the entities are packaged into the deployment artifact.

More info on doing so here: https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Static_Weaving

0
votes

thanks , i found out the problem , it was in declaring the ArrayList of staff , it has a problem when I persist Collection declared as ArrayList or HashSet , I should declare it as the super interface eg Set or List ,so I modified it to

  @OneToMany (targetEntity = Staff.class)
  private List<Staff> staffs;

so, it worked very well and when I built the jar file it worked without any problems