Java 使用 EclipseLink 预部署 PersistenceUnit 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18944362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:47:02  来源:igfitidea点击:

Predeployment of PersistenceUnit failed with EclipseLink

javajpaeclipselink

提问by Ruschel

Am new to JPA and starting out with EclipseLink. Taking help from tutorial I started creating my first JPA project consiting of an Entity (automatically created using JPA tools), a simple main class firing the most basic select query. However when i try running the main class I get a Predeployment of PersistenceUnit failed exception. Am using SQL server express 2008 and querying sample AdventureWorks database. Am able to connect to database (using Database Development Perspective). Detailed error log and Code snippets below:

我是 JPA 的新手并从 EclipseLink 开始。在教程的帮助下,我开始创建我的第一个 JPA 项目,它包含一个实体(使用 JPA 工具自动创建),一个简单的主类触发最基本的选择查询。然而,当我尝试运行主类时,我得到了 PersistenceUnit 的 Predeployment failed 异常。正在使用 SQL server express 2008 并查询示例 AdventureWorks 数据库。能够连接到数据库(使用数据库开发视角)。详细的错误日志和代码片段如下:

Main class

主班

    import java.util.List;
    import javax.persistence.EntityManager;
    import javax.persistence.Persistence;
    import model.Employee;

    public class Main {
    public static void main(String[] args) {
EntityManager em =    Persistence.createEntityManagerFactory("JPA2").createEntityManager();
    List<Employee> list = em.createQuery("Select * from humanresources.Employee", Employee.class).getResultList();
    for(Employee e : list)
    {
        System.out.println(e.getBirthDate());
    }
}
 }

Entity

实体

         package model;

         import java.io.Serializable;
         import javax.persistence.*;
         import java.sql.Timestamp;

        @Entity
        public class Employee implements Serializable { 
    private static final long serialVersionUID = 1L;

    @Id 
        @Column(name="BusinessEntityID")
    private int businessEntityID;

    @Column(name="BirthDate")
    private Object birthDate;

    @Column(name="Gender")
    private Object gender; 

    @Column(name="HireDate")
    private Object hireDate;

    @Column(name="JobTitle")
    private Object jobTitle;

  @Column(name="MaritalStatus");
    private Object maritalStatus;

@Column(name="ModifiedDate")
private Timestamp modifiedDate;

@Column(name="NationalIDNumber")
private Object nationalIDNumber;

@Column(name="OrganizationLevel")
private short organizationLevel;

@Column(name="OrganizationNode")
private String organizationNode;

private String rowguid;

@Column(name="SickLeaveHours")
private short sickLeaveHours;

@Column(name="VacationHours")
private short vacationHours;

public Employee() {
}

public int getBusinessEntityID() {
    return this.businessEntityID;
}

public void setBusinessEntityID(int businessEntityID) {
    this.businessEntityID = businessEntityID;
}

public Object getBirthDate() {
    return this.birthDate;
}

public void setBirthDate(Object birthDate) {
    this.birthDate = birthDate;
}

public Object getGender() {
    return this.gender;
}

public void setGender(Object gender) {
    this.gender = gender;
}

public Object getHireDate() {
    return this.hireDate;
}

public void setHireDate(Object hireDate) {
    this.hireDate = hireDate;
}

public Object getJobTitle() {
    return this.jobTitle;
}

public void setJobTitle(Object jobTitle) {
    this.jobTitle = jobTitle;
}


public Object getMaritalStatus() {
    return this.maritalStatus;
}

public void setMaritalStatus(Object maritalStatus) {
    this.maritalStatus = maritalStatus;
}

public Timestamp getModifiedDate() {
    return this.modifiedDate;
}

public void setModifiedDate(Timestamp modifiedDate) {
    this.modifiedDate = modifiedDate;
}

public Object getNationalIDNumber() {
    return this.nationalIDNumber;
}

public void setNationalIDNumber(Object nationalIDNumber) {
    this.nationalIDNumber = nationalIDNumber;
}

public short getOrganizationLevel() {
    return this.organizationLevel;
}

public void setOrganizationLevel(short organizationLevel) {
    this.organizationLevel = organizationLevel;
}

public String getOrganizationNode() {
    return this.organizationNode;
}

public void setOrganizationNode(String organizationNode) {
    this.organizationNode = organizationNode;
}

public String getRowguid() {
    return this.rowguid;
}

public void setRowguid(String rowguid) {
    this.rowguid = rowguid;
}

public short getSickLeaveHours() {
    return this.sickLeaveHours;
}

public void setSickLeaveHours(short sickLeaveHours) {
    this.sickLeaveHours = sickLeaveHours;
}

public short getVacationHours() {
    return this.vacationHours;
}

public void setVacationHours(short vacationHours) {
    this.vacationHours = vacationHours;
}

 }

Persistence.XML (under Meta-INF folder)

Persistence.XML(在 Meta-INF 文件夹下)

       <?xml version="1.0" encoding="UTF-8"?>
       <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="JPA2" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>model.Employee</class>
    <properties>
        <property name="eclipselink.target-database" value="SQLServer"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:1433;databaseName=AdventureWorks2008"/>
        <property name="javax.persistence.jdbc.user" value="Username"/>
        <property name="javax.persistence.jdbc.password" value="Username"/>
        <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
    </properties>
</persistence-unit>

Exception

例外

Exception in thread "main" Local Exception Stack: 
           Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.4.2.v20130514-                  5956486): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
           Exception Description: An exception was thrown while searching for persistence        archives with ClassLoader: sun.misc.Launcher$AppClassLoader@1ac04e8
           Internal Exception: javax.persistence.PersistenceException: Exception   [EclipseLink-28018] (Eclipse Persistence Services - 2.4.2.v20130514-5956486):          org.eclipse.persistence.exceptions.EntityManagerSetupException
           Exception Description: Predeployment of PersistenceUnit [JPA2] failed.
           Internal Exception: Exception [EclipseLink-7155] (Eclipse Persistence Services -  2.4.2.v20130514-5956486): org.eclipse.persistence.exceptions.ValidationException
           Exception Description: The type [class java.lang.Object] for the attribute  [hireDate] on the entity class [class model.Employee] is not a valid type for a serialized  mapping. The attribute type must implement the Serializable interface.
            at   org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPer sistenceResources(PersistenceUnitLoadingException.java:127)
            at  org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvi der.java:118)
            at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:78)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at Main.main(Main.java:20)
           Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018]  (Eclipse Persistence Services - 2.4.2.v20130514-5956486):  org.eclipse.persistence.exceptions.EntityManagerSetupException
           Exception Description: Predeployment of PersistenceUnit [JPA2] failed.
           Internal Exception: Exception [EclipseLink-7155] (Eclipse Persistence Services -  2.4.2.v20130514-5956486): org.eclipse.persistence.exceptions.ValidationException
           Exception Description: The type [class java.lang.Object] for the attribute   [hireDate] on the entity class [class model.Employee] is not a valid type for a serialized  mapping. The attribute type must implement the Serializable interface.
            at  org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenc eException(EntityManagerSetupImpl.java:1556)
            at  org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImp l.java:1547)
            at  org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer .java:98)
            at  org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvi der.java:108)
    ... 3 more
           Caused by: Exception [EclipseLink-28018] (Eclipse Persistence Services -  2.4.2.v20130514-5956486): org.eclipse.persistence.exceptions.EntityManagerSetupException
           Exception Description: Predeployment of PersistenceUnit [JPA2] failed.
           Internal Exception: Exception [EclipseLink-7155] (Eclipse Persistence Services -             2.4.2.v20130514-5956486): org.eclipse.persistence.exceptions.ValidationException
           Exception Description: The type [class java.lang.Object] for the attribute  [hireDate] on the entity class [class model.Employee] is not a valid type for a serialized   mapping. The attribute type must implement the Serializable interface.
            at  org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManage rSetupException.java:221)
                ... 7 more
           Caused by: Exception [EclipseLink-7155] (Eclipse Persistence Services -  2.4.2.v20130514-5956486): org.eclipse.persistence.exceptions.ValidationException
           Exception Description: The type [class java.lang.Object] for the attribute  [hireDate] on the entity class [class model.Employee] 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:1121)
    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:1807)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor.processJPA Converters(MappingAccessor.java:1586)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor.processMappingConverter(MappingAccessor.java:1652)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.MappingAccessor.processMap pingValueConverter(MappingAccessor.java:1670)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.BasicAccessor.process(BasicAccessor.java:414)
    at org.eclipse.persistence.internal.jpa.metadata.MetadataDescriptor.processMappingAccessors(MetadataDescriptor.java:1461)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor.processMappingAccessors(ClassAccessor.java:1526)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processMappingAccessors(EntityAccessor.java:1085)
    at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.process(EntityAccessor.java:645)
    at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage2(MetadataProject.java:1718)
    at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:536)
    at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:550)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1484)
    ... 5 more

回答by Romski

Part of the learning process is being used to reading stack traces. The line you need is:

部分学习过程用于读取堆栈跟踪。您需要的线路是:

Exception Description: The type [class java.lang.Object] for the attribute  [hireDate] on the entity class [class model.Employee] is not a valid type for a serialized mapping. The attribute type must implement the Serializable interface.

What does your Employeeclass look like?

你的Employee班级是什么样的?

EDIT

编辑

as Chris has noted your attributes are not Serializable. Make your dates into java.util.Date, java.util.Calendar or java.sql.Date. The first 2 require an @Temporal annotation with a TemporalType. Each of these would allow your example to run, but you should be aware of the differences in what will get stored.

正如 Chris 所指出的,您的属性不可序列化。将日期输入 java.util.Date、java.util.Calendar 或 java.sql.Date。前 2 个需要带有 TemporalType 的 @Temporal 注释。这些中的每一个都可以让您的示例运行,但您应该了解将要存储的内容的差异。

You should also change your other attributes to Serializable types such as String for jobTitle.

您还应该将其他属性更改为可序列化类型,例如 jobTitle 的 String。

hope this helps

希望这可以帮助

回答by user2254180

Did you ever get this sorted?

你有没有得到这个排序?

I noticed that in your entity class, your Employee entity is in a package called model (the default package created by the IDE) whereas in your persistence.xml and main method - you have it in a humanresource package.

我注意到在您的实体类中,您的 Employee 实体位于一个名为 model 的包(IDE 创建的默认包)中,而在您的 persistence.xml 和 main 方法中 - 您在人力资源包中拥有它。