0

I'm trying out this hibernate, and following a tutorial on it. However I quickly get stuck on an exception.

public class input {

public static void main(String[] args){

    student student = new student();
    student.setName("bar");
    student.setRollNo(1);

    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    session.save(student);

    session.getTransaction().commit();
    session.close();
    sessionFactory.close();
}
}

my student class

@Entity
public class student {

@Id
private int rollNo;
private String name;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getRollNo() {
    return rollNo;
}
public void setRollNo(int rollNo) {
    this.rollNo = rollNo;
}
}

UPDATED - console gives:

sep. 23, 2015 3:04:41 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.0.Final}
sep. 23, 2015 3:04:41 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
sep. 23, 2015 3:04:41 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 8 and column 63 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:135)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:67)
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:55)
    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.test.input.main(input.java:15)
Caused by: javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 63; 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:128)
    ... 6 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 63; 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)
    ... 8 more

I'm not sure what is causing this. I tried to make a normal prepared statement, and suceeded. So I'm nearly sudden it's not my database, or login details that is the problem.

hibernate.cfg.xml file

<?xml version="1.0" encoding="UTF-8"?> 

<hibernate-configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
    xmlns="http://www.hibernate.org/xsd/hibernate-configuration">

    <session-factory>

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="connection.url">jdbc:mysql://localhost:3306/dbname</property>

        <property name="connection.username">someuser</property>

        <property name="connection.password"/>somepass</property>

        <property name="connection.pool_size">1</property>

        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <property name="current_session_context_class">thread</property>

        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
        <property name="show_sql">true</property>

        <property name="hbm2ddl.auto">create</property>

        <mapping class="com.test.student"/>

    </session-factory>

</hibernate-configuration>
Bacteria
  • 8,406
  • 10
  • 50
  • 67
JonCode
  • 241
  • 1
  • 8
  • 20
  • Can you post your hbm file? – BackSlash Sep 23 '15 at 12:44
  • 1
    The error indicates that it was trying to parse some config XML file but failed. Seems that the XML is not valid. – NickJ Sep 23 '15 at 12:47
  • Looks like a invalid XML configuration. – Hardy Sep 23 '15 at 12:48
  • What tutorial are you using? The log seems to imply that you are using the latest version of Hibernate ORM (ORM 5). What version does your tutorial use? – Hardy Sep 23 '15 at 12:49
  • I was blindly following https://www.youtube.com/watch?v=FFMOZY4z6bE&list=PL4AFF701184976B25&index=5. I posted my hibernate config file. – JonCode Sep 23 '15 at 12:51
  • Are those dashes (as in line 2) really part of your xml file? – Marvin Sep 23 '15 at 12:59
  • @ Marvin it should not be. @JonCode could u plz remove these two dashes and try to run ur program? – Bacteria Sep 23 '15 at 13:03
  • I actually just noticed that. However removing it gives another error. I edited my post to contain the new log. This one does not seem to be any easier than the last one to read for me :l – JonCode Sep 23 '15 at 13:06
  • Try to add _hibernate_ in front of every property name, like hibernate.connection.driver_class, hibernate.connection.url, etc. – drgPP Sep 23 '15 at 13:34
  • Done and it gave "Unable to perform unmarshalling at line number 5 and column 62 in RESOURCE hibernate.cfg.xml Message: cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'. :/ – JonCode Sep 23 '15 at 13:43
  • @JonCode check [this](http://stackoverflow.com/questions/8640619/hibernate-serviceregistrybuilder) post, may be you will find solution to your problem there. – drgPP Sep 23 '15 at 14:00
  • I will check it out, thanks. – JonCode Sep 23 '15 at 14:23

1 Answers1

3

There is a fault in line number 8 of hibernate.cfg.xml file so it is failed to parse the xml file. Please look at the following line carefully.

<property name="connection.password"/>somepass</property>

then modify it to

<property name="connection.password">somepass</property>

this is not a hibernate fault, it's only a simple xml fault.

Channa Jayamuni
  • 1,876
  • 1
  • 18
  • 29
  • That was exactly it!. Nice s potted, and sorry for the late response, I went to sleep. Thanks to all for helping out :) – JonCode Sep 24 '15 at 01:29