2016-04-13 380 views
0

我正在將java對象映射到數據庫(我正在使用postgres)。當我運行java文件(HibernateTest.java)時,它顯示了很多錯誤。我認爲所有的事情都是正確的。但我對我的eclipse有懷疑,bcoz它不支持xml(我的意思是它沒有顯示顏色,它沒有把xml文件當作知道文件)。我是初學者,所以我不能理解他們。 PLZ建議我。將java對象映射到數據庫

hibernate.cfg.xml文件

<?xml version='1.0' encoding='utf-8'?> 

    <hibernate-configuration 
     xmlns="http://www.hibernate.org/xsd/hibernate-configuration" 
     xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <session-factory> 
     <!-- Database connection settings --> 
     <property name="connection.driver_class">org.postgresql.Driver</property> 
     <property name="connection.url">jdbc:hsqldb:hsql://localhost:5432/hibernatedb</property> 
    <property name="connection.username">postgres</property> 
    <property name="connection.password">[email protected]</property> 

    <!-- JDBC connection pool (use the built-in) --> 
    <property name="connection.pool_size">1</property> 

    <!-- SQL dialect --> 
    <property name="dialect">org.hibernate.dialect.PostgreSQlDialect</property> 

    <!-- Enable Hibernate's automatic session context management --> 
    <property name="current_session_context_class">thread</property> 

    <!-- Disable the second-level cache --> 
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> 

    <!-- Echo all executed SQL to stdout --> 
    <property name="show_sql">true</property> 

    <!-- Drop and re-create the database schema on startup --> 
    <property name="hbm2ddl.auto">create</property> 
    <mapping resource="org.hibernate.hemant.dto.UDestails"/> 
    </session-factory> 
</hibernate-configuration> 

UserDetails.java文件

package org.hibernate.hemant.dto; 
    import javax.persistence.Entity; 
    import javax.persistence.Id; 

    @Entity 
    public class UserDetails 
    { 
    @Id 
    private int userID; 
    private String userName; 
    public int getUserID() { 
    return userID; 
    } 
    public void setUserID(int userID) { 
    this.userID = userID; 
    } 
    public String getUserName() { 
    return userName; 
    } 
    public void setUserName(String userName) { 
    this.userName = userName; 
    } 
} 

我主要的Java文件HibernateTest.java

package org.hemant.hibernate; 
    import org.hibernate.Session; 
    import org.hibernate.SessionFactory; 
    import org.hibernate.cfg.Configuration; 
    import org.hibernate.hemant.dto.UserDetails; 
    public class HibernateTest { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
    UserDetails user = new UserDetails(); 
    user.setUserID(1); 
    user.setUserName("First user"); 
    SessionFactory sessionFactory = new   Configuration().configure().buildSessionFactory(); 
    Session session = sessionFactory.openSession(); 
    session.beginTransaction(); 
    session.save(user); 
    session.beginTransaction().commit(); 
} 

}

有錯誤......

Apr 13, 2016 9:26:08 PM org.hibernate.Version logVersion 
    INFO: HHH000412: Hibernate Core {5.1.0.Final} 
    Apr 13, 2016 9:26:08 PM org.hibernate.cfg.Environment <clinit> 
    INFO: HHH000206: hibernate.properties not found 
    Apr 13, 2016 9:26:08 PM org.hibernate.cfg.Environment buildBytecodeProvider 
    INFO: HHH000021: Bytecode provider name : javassistException in thread "main" 
    Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 13 and column 63 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'. 
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133) 
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65) 
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57) 
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163) 
at org.hibernate.cfg.Configuration.configure(Configuration.java:259) 
at org.hibernate.cfg.Configuration.configure(Configuration.java:245) 
at org.hemant.hibernate.HibernateTest.main(HibernateTest.java:15) 
Caused by: javax.xml.bind.UnmarshalException 
    - with linked exception: 
[org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 63; cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.] 
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(Unknown Source) 
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source) 
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source) 
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:126) 
... 6 more 
    Caused by: org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 63; cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'. 
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source) 
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(Unknown Source) 
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(Unknown Source) 
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(Unknown Source) 
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(Unknown Source) 
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.handleStartElement(Unknown Source) 
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.bridge(Unknown Source) 
... 9 more     
+0

你必須更加明確你的錯誤。 – Zakaria

+0

對不起,我忘了添加錯誤....現在我已經把所有的錯誤。 @Zakaria – Hemant

回答

0

異常的原因是不正確hibernate.cfg.xml文件。 請使用此結構

<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

    <hibernate-configuration> 
     <session-factory> 
      ... 
      <mapping class="org.hibernate.hemant.dto.UDestails" /> 
     </session-factory> 
    </hibernate-configuration> 

hibernate.cfg.xml你不正確指定一個持久化類。您可以使用帶有註釋的映射,所以你需要映射class不是resource

<mapping class="org.hibernate.hemant.dto.UDestails" />

請不要忘記關閉一個sessionsessionFactory