简体   繁体   中英

Getting error while inserting data to MySQL using Hibernate

HI I am developing a project in eclipse using Hibernate. I found this Error in my hibernate.cfg.xml file

Please help with this problem.

I have included all the necessary libraries.

hibernate.cfg.xml

    <hibernate-configuration>
    <session-factory>



        <!-- Related to the connection START -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.user">root</property>
        <property name="connection.password">93Pb3gaNv0</property>
        <!-- Related to the connection END -->

        <!-- Related to hibernate properties START -->
        <property name="show_sql">true</property>
        <property name="dailet">org.hibernate.dialect.MySQLDialect</property>
        <property name="hbm2ddl.auto">update </property>
        <!-- Related to hibernate properties END -->

        <!-- Related to mapping START -->
        <mapping resource="user.hbm.xml"/>
        <!-- Related to mapping END -->




    </session-factory>
</hibernate-configuration>

user.hbm.xml

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

      <hibernate-mapping>
      <class name="mypack.DataProvider" table="user_info">
      <id name="user_id" column="id">
      <generator class="assinged"></generator>
      </id>
      <property name="user_name" column="name"></property>
       <property name="user_address" column="address"></property>
      </class>
      </hibernate-mapping>

DataProvider.java

 package mypack;

public class DataProvider {

private int user_id;
public int getUser_id() {
    return user_id;
}
public void setUser_id(int user_id) {
    this.user_id = user_id;
}
public String getUser_name() {
    return user_name;
}
public void setUser_name(String user_name) {
    this.user_name = user_name;
}
public String getUser_address() {
    return user_address;
}
public void setUser_address(String user_address) {
    this.user_address = user_address;
}
private String user_name;
private String user_address;
}

DataInsertion.java

package mypack;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class DataInsertion {

public static void main(String[] args) {
    new DataInsertion().insertInfo();

}

public void insertInfo()
{
Configuration con = new Configuration();
con.configure("hibernate.cfg.xml");
SessionFactory SF = con.buildSessionFactory();
Session session = SF.openSession();
DataProvider provider = new DataProvider();
provider.setUser_id(121);
provider.setUser_name("Mehandi Hassan");
provider.setUser_address("Delhi");
Transaction TR = session.beginTransaction();
session.save(provider);
System.out.println("Object save successfully");
TR.commit();
session.close();
SF.close(); 
}
}

Error:

May 29, 2015 5:34:46 PM                   org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
 INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
 May 29, 2015 5:34:46 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.10.Final}
 May 29, 2015 5:34:46 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
May 29, 2015 5:34:46 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
May 29, 2015 5:34:46 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
May 29, 2015 5:34:46 PM org.hibernate.cfg.Configuration        
getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
 Exception in thread "main" org.hibernate.MappingException: invalid    
configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2077)
at mypack.DataInsertion.insertInfo(DataInsertion.java:18)
at mypack.DataInsertion.main(DataInsertion.java:11)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 25;    
Document is invalid: no grammar found.
at 


   com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.
    createSAXParseExcepti       on    (Unknown Source)
   at   com.sun.org.apache.xerces.internal.util.
    ErrorHandlerWrapper.error(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.      
   XMLErrorReporter.reportError(Unknown Source)
   at com.sun.org.apache.xerces.internal.    
   impl.XMLErrorReporter.reportError(Unknown Source)
    at    com.sun.org.apache.xerces.internal.
   impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.
    impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
     at com.sun.org.apache.xerces.internal.
    impl.XMLNSDocumentScannerImpl$NSContentDriver.  
   scanRootElementHook(Unknown Source)
   at com.sun.org.apache.xerces.internal.impl.
   XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
   at com.sun.org.apache.xerces.internal.
   impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.
   impl.XMLDocumentScannerImpl.next(Unknown Source)
     at com.sun.org.apache.xerces.internal.
     impl.XMLNSDocumentScannerImpl.next(Unknown Source)
     at com.sun.org.apache.xerces.internal.

    impl.XMLDocumentFragmentScannerImpl.
     scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.
  internal.parsers.XML11Configuration.parse(Unknown Source)
   at com.sun.org.apache.xerces.internal.
   parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.
    parsers.XMLParser.parse(Unknown Source)
     at com.sun.org.apache.xerces.internal.
    parsers.AbstractSAXParser.parse(Unknown Source)
   at com.sun.org.apache.xerces.
  internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
  at org.dom4j.io.SAXReader.read(SAXReader.java:465)
   at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2157)
  ... 3 more

You need to annotate DataProvider with @Entity and the id with @Id

@Entity public class DataProvider { @Id private int user_id; @Column private String user_name; @Column private String user_address;

... setters getters }

With Hibernate 4.x you should use the same DTD you need to add in config file hibernate.cfg.xml

 <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

    <hibernate-configuration>

        <session-factory>

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.user">root</property>
        <property name="connection.password">93Pb3gaNv0</property>

        ....
        ....

    </session-factory>
</hibernate-configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM