2013-09-22 7 views
5

जेपीए में नया है और एक्लिप्ससेंक से शुरू हो रहा है। ट्यूटोरियल से मदद लेते हुए मैंने अपनी पहली जेपीए परियोजना को एक इकाई (स्वचालित रूप से जेपीए टूल्स का उपयोग करके बनाया गया) बनाने की शुरुआत की, एक साधारण मुख्य वर्ग सबसे बुनियादी चयन क्वेरी को फायरिंग करना शुरू कर दिया। हालांकि जब मैं मुख्य वर्ग को चलाने का प्रयास करता हूं तो मुझे दृढ़ता की एक पूर्वनिर्धारितता मिलती है अपूर्ण अपवाद। मैं SQL सर्वर एक्सप्रेस 2008 का उपयोग कर रहा हूँ और नमूना AdventureWorks डेटाबेस पूछताछ कर रहा हूँ। डेटाबेस से कनेक्ट करने में सक्षम हूं (डेटाबेस विकास परिप्रेक्ष्य का उपयोग कर)। विस्तृत त्रुटि लॉग और नीचे दिए कोड स्निपेट:PersistenceUnit की पूर्वनिर्धारित EclipseLink

मुख्य वर्ग

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

इकाई

  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 (मेटा-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 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: [email protected] 
      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 

उत्तर

3

सीखने की प्रक्रिया का एक हिस्सा स्टैक ट्रेस पढ़ने के लिए इस्तेमाल किया जा रहा है। आपको जिस लाइन की आवश्यकता है वह है:

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. 

आपकी Employee कक्षा कैसी दिखती है?

संपादित

के रूप में क्रिस उल्लेख किया गया है अपनी विशेषताओं Serializable नहीं हैं। अपनी तिथियां java.util.Date, java.util.Calendar या java.sql.Date में करें। पहले 2 को एक टेम्पोरल टाइप के साथ @ टेम्पोरल एनोटेशन की आवश्यकता होती है। इनमें से प्रत्येक आपके उदाहरण को चलाने की अनुमति देगा, लेकिन आपको संग्रहित होने वाले अंतरों के बारे में अवगत होना चाहिए।

आपको जॉबटाइटल के लिए स्ट्रिंग जैसे सीरियलज़ेबल प्रकारों में अपने अन्य विशेषताओं को भी बदलना चाहिए।

उम्मीद है कि यह

+0

जैसा कि मैंने बताया है कि एंटीटी क्लास जेपीए का उपयोग करके ऑटो-जेनरेट की गई थी और इसलिए डिफ़ॉल्ट रूप से सीरियलज़ेबल लागू करता है। इसके अलावा मैंने एंटिटी क्लास को एक ही int संपत्ति में अलग करने की कोशिश की है, लेकिन अभी भी कोई भाग्य नहीं है। मैंने – Ruschel

+0

से ऊपर के प्रश्न में एंटीटी क्लास के लिए विवरण भी जोड़े हैं, कर्मचारी के भीतर आपके सभी गुण प्रकार ऑब्जेक्ट्स प्रकार हैं जो क्रमिक नहीं हैं, और संभवतः आप जो चाहते हैं उसकी संभावना नहीं है। उदाहरण के लिए त्रुटि में उल्लिखित किराए पर लगता है जैसे यह java.sql.Date होना चाहिए – Chris

2

क्या आपको कभी भी यह सॉर्ट किया गया है?

मैंने देखा है कि आपकी इकाई वर्ग में, आपकी कर्मचारी इकाई मॉडल (आईडीई द्वारा निर्मित डिफ़ॉल्ट पैकेज) नामक पैकेज में है जबकि आपके persistence.xml और मुख्य विधि में - आपके पास यह एक मानव संसाधन पैकेज में है।

संबंधित मुद्दे