JonCode
JonCode

Reputation: 241

Java hibernate and mysql

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>

Upvotes: 0

Views: 3319

Answers (1)

Channa Jayamuni
Channa Jayamuni

Reputation: 1905

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.

Upvotes: 3

Related Questions