java - 线程 "main"org.hibernate.InvalidMappingException : Unable to read XML 中出现异常

标签 java xml eclipse hibernate class

  1. > 我正在使用 eclipse。我第一次尝试编写 java hibernate 程序,但出现了上述错误。请帮我解决它。

    downloaded hibernate release 4.2.5 final.zip and added jar files to user library
    antlr-2.7.7.jar
    dom4j-1.6.1.jar
    hibernate-commons-annotations-4.0.2.Final.jar
    hibernate-core-4.2.5.Final.jar
    hibernate-jpa-2.0-api-1.0.1.Final.jar
    javassist-3.15.0-GA.jar
    jboss-logging-3.1.0.GA.jar
    jboss-transaction-api_1.1_spec-1.0.1.Final.jar
    hibernate-entitymanager-4.2.5.Final.jar
    hibernate-envers-4.2.5.Final.jar
    hibernate-osgi-4.2.5.Final.jar
    org.osgi.core-4.3.1.jar
    


    is these jar files enough for hibernate program.

hibernate.cfg.xml

          <?xml version='1.0' encoding='UTF-8'?>
          <!DOCTYPE hibernate-configuration PUBLIC
           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

             <hibernate-configuration>
           <session-factory>

           <!-- Related to the connection START -->
             <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver
             </property>
           <property name="connection.url">jdbc:oracle:thin:@192.168.1.2:1521:xe</property>
             <property name="connection.user">system</property>
             <property name="connection.password">siddhu</property>
            <!-- Related to the connection END -->

             <!-- Related to hibernate properties START -->
             <property name="show_sql">true</property>
             <property name="dialet">org.hibernate.dialect.OracleDialect</property>
           <property name="hbm2ddl.auto">update</property>
           <!-- Related to hibernate properties END -->

           <!-- Related to mapping START -->
           <mapping resource="Details.hbm.xml"/>
           <!-- Related to the mapping END -->

          </session-factory>
          </hibernate-configuration>

详细信息.hbm.xml

             -       <hibernate-mapping>
             -       <class name="Details" table="stu">
             -       <id name="stNo" column="SNo">
            <generator class="assigned" /> 
            </id>
            <property name="stName" column="SName" /> 
            <property name="stAddress" /> 
            </class>
            </hibernate-mapping>

mainclass.java

    import org.hibernate.*;
    import org.hibernate.cfg.*;
    public class mainclass {
         public static void main(String[] args)
            {

                Configuration c = new Configuration();
                c.configure("hibernate.cfg.xml"); 

                SessionFactory factory = c.buildSessionFactory();
                Session session = factory.openSession();
              Details p=new Details();

                p.setStno(101);
                p.setStName ("iPhone");
                p.setStAddress("dfdsgdf");

                Transaction tx = session.beginTransaction();
                session.save(p);
                System.out.println("Object saved successfully.....!!");
                tx.commit();
                session.close();
                factory.close();
            }
    }

详细信息.java

    public class Details {
        private int stNo;
        private String stName;
        private String stAddress;

             public void setStno(int stNo)
             {
             this.stNo=stNo;
             }
             public int getStNo()
             {
             return stNo;
             }

             public void setStName(String stName)
             {
             this.stName=stName;
             }
             public String getStName()
             {
             return stName;
             }

             public void setStAddress(String stAddress)
             {
             this.stAddress=stAddress;
             }
             public String getStAddress()
             {
             return stAddress;
             }


    }



 Console Error
===========================================


             Sep 20, 2013 6:49:30 PM org.hibernate.annotations.common.Version <clinit>
           INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
           Sep 20, 2013 6:49:30 PM org.hibernate.Version logVersion
             INFO: HHH000412: Hibernate Core {4.2.5.Final}
            Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Environment <clinit>
            INFO: HHH000206: hibernate.properties not found
            Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Environment buildBytecodeProvider
            INFO: HHH000021: Bytecode provider name : javassist
          Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Configuration configure
           INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
        Sep 20, 2013 6:49:30 PM org.hibernate.cfg.Configuration getConfigurationInputStream
            INFO: HHH000040: Configuration resource: hibernate.cfg.xml
          Sep 20, 2013 6:49:31 PM org.hibernate.internal.util.xml.DTDEntityResolver         resolveEntity
            WARN: HHH000223: Recognized obsolete hibernate namespace         http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead.    Refer to Hibernate 3.6 Migration Guide!
           Sep 20, 2013 6:49:31 PM org.hibernate.cfg.Configuration addResource
            INFO: HHH000221: Reading mappings from resource: Details.hbm.xml
           Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML
            at     org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
             at org.hibernate.cfg.Configuration.add(Configuration.java:488)
             at org.hibernate.cfg.Configuration.add(Configuration.java:484)
             at org.hibernate.cfg.Configuration.add(Configuration.java:657)
             at org.hibernate.cfg.Configuration.addResource(Configuration.java:740)
             at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2188)
            at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2160)
            at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2140)
            at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2093)
            at org.hibernate.cfg.Configuration.configure(Configuration.java:2008)
            at mainclass.main(mainclass.java:9)
            Caused by: org.dom4j.DocumentException: Error on line 1 of document  : Content   is not  allowed in prolog. Nested exception: Content is not allowed in prolog.
            at org.dom4j.io.SAXReader.read(SAXReader.java:482)
            at     org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
        ... 10 more

最佳答案

无论出于何种原因,您的 XML 文件可能包含 this answer and its linked article. 中所述的 BOM

我建议您创建一个新文件并写入(复制)映射文件中的配置。

关于java - 线程 "main"org.hibernate.InvalidMappingException : Unable to read XML 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18921054/

相关文章:

xml - XML 映射文件的 Hibernate @embeddable 注释等效?

c# - 使用命名空间别名/前缀的 XmlReader 查询问题

java - Eclipse Platform.getBundle() 的纯 OSGi 等价物是什么

linux - 在 Windows 上开发并在 linux 机器上运行

java - 将 NetBeans IDE 的数据库管理器与 UCanAccess JDBC 驱动程序一起使用

java - 将 Queue 和 Stack 相互复制

xml - 需要 XML 模式的小数位数

java - Spring Cloud kubernetes 使用 feign 的正确方法是什么?

Java多线程问题.join()

eclipse - Eclipse:如何使目录树显示为包