0

我正在尝试将休眠 5 配置为在 Eclipse 中访问 oracle 11 g。

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]

通过使用 @Entity 和 userId 作为主键使用 @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

以下课程尝试将 Hello 的实例保存在数据库中。

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
4

1 回答 1

0

错误消息告诉您它不希望<property>某处有元素。相反,它需要一个<mapping>元素,或者一个<class-cache>元素,或者......

实际上,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>

所以你在<mapping>元素中间有一个<property>元素。因此这是无效的。

于 2016-01-17T13:30:06.427 回答