2

Trying to create a simple student database with a single entry in it. I am using Hibernate 5.1.0 and MySql as database with simple student table, and cannot connect to the database. Using Eclipse Mars and also MYSQL jdbc driver added to the project along with hibernate 5.1 necessary jars.

My StudentInfo.java file

package com.nitish.hibernate;

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

@Entity
@Table(name="student")
public class StudentInfo {
    @Id
    private int rollno;
    private String name;
    private int marks;

    public int getRollno() {
        return rollno;
    }
    public void setRollno(int rollno) {
        this.rollno = rollno;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getMarks() {
        return marks;
    }
    public void setMarks(int marks) {
        this.marks = marks;
    }


}

my Main.java File

package com.nitish.hibernate;

import org.hibernate.*; 

public class Main {

    public static void main(String args[]){

        StudentInfo s1 = new StudentInfo();

        s1.setName("Nitishpisal");
        s1.setRollno(5);
        s1.setMarks(33);


        try{
            Configuration conf = new Configuration();
            System.out.println("Hibernate Configuration loaded"); 
            conf.configure("hibernate.cfg.xml");

            ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
            System.out.println("registry loaded"); 

            SessionFactory sf = conf.buildSessionFactory(sr);
            System.out.println(" session factory loaded");

            Session session = sf.openSession();

            session.beginTransaction();
            session.save(s1);
            session.getTransaction().commit();
            }catch(HibernateException e){
                e.printStackTrace();
            }   

    }
}

My hibernate.cfg.xml file

<!DOCTYPE hibernate-configuration PUBLIC 
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration xmlns="http://www.hibernate.org/xsd/hibernate-configuration">


    <session-factory>


        <!--  Database connection settings -->  -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property 
name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.pool_size">1</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>

        <!-- model classes  and mapping info-->
        <mapping class="com.nitish.hibernate.StudentInfo"></property>

    </session-factory>

</hibernate-configuration>

The error I am getting

May 23, 2016 2:16:51 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
May 23, 2016 2:16:51 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Hibernate Configuration loaded
May 23, 2016 2:16:51 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 5 and column 87 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.
    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 com.nitish.hibernate.Main.main(Main.java:27)
Caused by: javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 87; cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:468)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:448)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:420)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:126)
    ... 5 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 87; cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1906)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:746)
    at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(ValidatorHandlerImpl.java:570)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:86)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.handleStartElement(StAXEventConnector.java:246)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.bridge(StAXEventConnector.java:115)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:445)
    ... 7 more

any help will be highly appreciated!

nitishpisal
  • 143
  • 1
  • 2
  • 9
  • Worked when I replaced my Main.java file with this SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(s1); session.getTransaction().commit(); Thank you – nitishpisal May 24 '16 at 18:58

2 Answers2

2

Try to remove this string: xmlns="http://www.hibernate.org/xsd/hibernate-configuration"

Oleg
  • 49
  • 10
  • seems like it didn't like schema: `Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.` Try to replace `"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"` to this `http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd` Надеюсь поможет:) – Oleg Aug 31 '17 at 05:38
-2

Remove closing property tag after declaration of mapping class in hibernate.cfg.xml Replace

<!-- model classes  and mapping info-->
<mapping class="com.nitish.hibernate.StudentInfo"></property>

with

<!-- model classes  and mapping info-->
 <mapping class="com.nitish.hibernate.StudentInfo">
Anish Barnwal
  • 152
  • 1
  • 6