1

  1. > I am using eclipse .I tried to do java hibernate program for the first time but got the above error.please help me to solve it.
    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>

Details.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();
            }
    }

Details.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
user2799436
  • 36
  • 1
  • 4

1 Answers1

0

Your XML file, for whatever reason, might contain a BOM as described in this answer and its linked article.

I suggest you create a new file and write (copy) over the configuration from your mapping file.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • what is meant by class path – user2799436 Sep 20 '13 at 16:31
  • @user2799436 Classpath is the group of directories where the `java` launcher and compiler look for `class` files to load. Google it for much more details. – Sotirios Delimanolis Sep 20 '13 at 16:51
  • guys error due to the statement SessionFactory sessionFactory = configuration.buildSessionFactory(); The method buildSessionFactory() from the type Configuration is deprecated please help me to rid off this error . At console following error persist Exception in thread "main" java.lang.NullPointerException at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:207) – user2799436 Sep 21 '13 at 02:01