1

  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
    


    这些 jar 文件是否足以用于休眠程序。

休眠.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>

主类.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
4

1 回答 1

0

无论出于何种原因,您的 XML 文件都可能包含此答案及其链接文章中所述的 BOM。

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

于 2013-09-20T16:05:19.230 回答