来自Jpa代码的Jar文件不起作用

时间:2017-09-21 14:52:35

标签: jpa executable-jar

我开发了一个简单的代码,通过使用Jpa在实体类和#34;员工"& "部"它在运行代码时工作正常,但是当我从应用程序创建一个jar文件时出现了真正的问题,jar文件中出现异常,我在txt文件中写了异常

这是Employee类

    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;
    }
    }

这是显示员工姓名和学位的班级

    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();

    }

这是例外 &#34; 异常[EclipseLink-28019](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.EntityManagerSetupException 异常说明:PersistenceUnit [Eclipselink_JPA]的部署失败。关闭此PersistenceUnit的所有工厂。 内部异常:异常[EclipseLink-0](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.IntegrityException

描述符例外:

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

异常说明:属性[teacherSet]未声明为ValueHolderInterface类型,但其映射使用间接。

映射:org.eclipse.persistence.mappings.ManyToManyMapping [teacherSet] 描述符:RelationalDescriptor(com.tutorialspoint.eclipselink.entity.Clas - &gt; [DatabaseTable(CLAS)])

异常[EclipseLink-1](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.DescriptorException 异常说明:属性[clasSet]未声明为ValueHolderInterface类型,但其映射使用间接。 映射:org.eclipse.persistence.mappings.ManyToManyMapping [clasSet] 描述符:RelationalDescriptor(com.tutorialspoint.eclipselink.entity.Teacher - &gt; [DatabaseTable(TEACHER)])

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

异常说明:属性[staffs]未声明为ValueHolderInterface类型,但其映射使用间接。 映射:org.eclipse.persistence.mappings.ManyToManyMapping [staffs]

描述符:RelationalDescriptor(com.tutorialspoint.eclipselink.entity.Employee - &gt; [DatabaseTable(EMPLOYEE)])

运行时异常: -------------------------------------------------- -------&#34;

那么可以做些什么?知道从IDE运行代码时程序运行良好但是当我构建它并创建jar文件并运行jar文件时会发生此异常

2 个答案:

答案 0 :(得分:0)

涉及ValueHolders和间接等接口的异常很可能是由于实体编织引起的问题。

实体编织是一个修改编译实体的字节码的过程,以便它们实现更多接口并添加新方法,以便它们可以处理诸如间接延迟加载,以及其他功能。

您的IDE是否是Oracle JDeveloper?默认情况下,它是其中一个IDE,它具有自动执行此操作的运行配置,以便您的实体正常工作。这可以在其他IDE中以类似的方式配置 - 通过添加-javaagent:<path to eclipselink JAR>作为程序参数(或某些IDE中的Java选项)。查看this blog post以获取一些快速信息。

在您的部署中,Eclipselink的动态(运行时)编织失败(或由于某种原因不完整)可能就是这种情况。在将实体打包到部署工件之前,您可能应该考虑静态编织。

有关这样做的更多信息:https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving/Static_Weaving

答案 1 :(得分:0)

谢谢,我发现了问题,它是在声明ArrayList的工作人员,它有一个问题,当我坚持将Collection声明为ArrayList或HashSet时,我应该将它声明为超级接口,例如Set或List,所以我修改了它

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

所以,它工作得非常好,当我构建jar文件时,它没有任何问题