2016-01-17 75 views
0

我想配置hibernate 5來在eclipse中訪問oracle 11 g。hibernate 5 oracle11g在eclipse中的配置

的hibernate.cfg.xml如下:

在下面的XML我已經指定的數據庫憑證(URL,用戶名和密碼),dilect,映射類,show_sql。我想問我是否錯過了任何重要的財產。

<?xml version='1.0' encoding='utf-8'?> 
<!-- 
    ~ Hibernate, Relational Persistence for Idiomatic Java 
    ~ 
    ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. 
    ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. 
    --> 
<!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> 

     <!-- Database connection settings --> 
     <!-- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
        <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> --> 

     <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
     <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:XE</property> 
     <property name="connection.username">SYSTEM</property> 
     <property name="connection.password">motog</property> 

     <mapping class="com.demo.one.Hello"/> 

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

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

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

     <property name="cache.use_query_cache">true</property> 
     <property name="cache.use_second_level_cache">true</property> 
     <property name="cache.use_structured_entries">true</property> 
     <property name="cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property> 
     <property name="net.sf.ehcache.configurationResourceName">/hibernate-config/ehcache.xml</property> 
     <!-- Echo all executed SQL to stdout --> 
     <property name="hibernate.show_sql">true</property> 

     <mapping resource="hibernate-config/domain/Event.hbm.xml"/> 
     <mapping resource="hibernate-config/domain/Person.hbm.xml"/> 
     <mapping resource="hibernate-config/domain/PhoneNumber.hbm.xml"/> 
     <mapping resource="hibernate-config/domain/Account.hbm.xml"/> 
     <mapping resource="hibernate-config/domain/HolidayCalendar.hbm.xml"/> 

     <mapping resource="hibernate-config/domain/Item.hbm.xml"/> 

    </session-factory> 

</hibernate-configuration> 


[image specifies project structure and imported libraries][1] 

定義Hello類如表名使用@Entity和用戶ID作爲主鍵使用@Id

Hello.java:

package com.demo.one; 

    import javax.persistence.Entity; 
    import javax.persistence.Id; 

    @Entity 
    public class Hello { 
     @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; 
    } 

    } // end of Hello.java 

下面的類試圖保存你好的實例中數據庫。

OracleTest.java:

package com.demo.one; 

    import org.hibernate.Session; 
    import org.hibernate.SessionFactory; 
    import org.hibernate.cfg.Configuration; 

    public class OracleTest { 

     public static void main(String[] args) { 
      Hello z = new Hello(); 
      z.setUserId(2); 
      z.setUserName("ashish"); 
      SessionFactory sf = new        Configuration().configure().buildSessionFactory(); 
      Session session = sf.openSession(); 
      session.beginTransaction(); 
      session.save(z); 
      session.getTransaction().commit(); 

     } 

    } // End of OracleTest.java 

我試圖挽救Hello類的對象的Oracle 11g XE數據庫(位於我的本地計算機上),OracleTest.java是該寫的。

我收到以下錯誤。

Jan 17, 2016 6:08:01 PM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {5.0.7.Final} 
Jan 17, 2016 6:08:01 PM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Jan 17, 2016 6:08:01 PM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number -1 and column -1 in RESOURCE hibernate.cfg.xml. Message: cvc-complex-type.2.4.a: Invalid content was found starting with element 'property'. One of '{"http://www.hibernate.org/xsd/orm/cfg":mapping, "http://www.hibernate.org/xsd/orm/cfg":class-cache, "http://www.hibernate.org/xsd/orm/cfg":collection-cache, "http://www.hibernate.org/xsd/orm/cfg":event, "http://www.hibernate.org/xsd/orm/cfg":listener}' is expected. 
    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 com.demo.one.OracleTest.main(OracleTest.java:13) 
Caused by: javax.xml.bind.UnmarshalException 
- with linked exception: 
[org.xml.sax.SAXParseException; cvc-complex-type.2.4.a: Invalid content was found starting with element 'property'. One of '{"http://www.hibernate.org/xsd/orm/cfg":mapping, "http://www.hibernate.org/xsd/orm/cfg":class-cache, "http://www.hibernate.org/xsd/orm/cfg":collection-cache, "http://www.hibernate.org/xsd/orm/cfg":event, "http://www.hibernate.org/xsd/orm/cfg":listener}' is expected.] 
    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; cvc-complex-type.2.4.a: Invalid content was found starting with element 'property'. One of '{"http://www.hibernate.org/xsd/orm/cfg":mapping, "http://www.hibernate.org/xsd/orm/cfg":class-cache, "http://www.hibernate.org/xsd/orm/cfg":collection-cache, "http://www.hibernate.org/xsd/orm/cfg":event, "http://www.hibernate.org/xsd/orm/cfg":listener}' is expected. 
    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$XSIErrorReporter.reportError(Unknown Source) 
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(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 

please guide me, its my first encounter with hibernate. 

PS: please check attached image. link is at the bottom. 

regards 
ashish 



    [1]: http://i.stack.imgur.com/DBYxu.png 

回答

0

錯誤消息告訴你,它不期待<property>元素的某處。相反,它需要一個<mapping>元素,或<class-cache>元素,或...

事實上,the DTD定義如下:

<!ELEMENT session-factory (property*, mapping*, (class-cache|collection-cache)*, event*, listener*)> 

這意味着,<session-factory>下,你應該有0或更多<property>元素,然後是0或更多<mapping>元素。

但是你的XML文件

<property name="connection.password">motog</property> 

<mapping class="com.demo.one.Hello"/> 

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

所以,你必須在<property>元素中間的<mapping>元素。因此這是無效的。