使用Hibernate将数据插入MySQL时出错

时间:2015-05-29 12:31:26

标签: java mysql spring hibernate jpa

我正在使用Hibernate在eclipse中开发一个项目。我在hibernate.cfg.xml文件中发现了这个错误

请帮助解决这个问题。

我已经包含了所有必要的库。

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

错误:

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

2 个答案:

答案 0 :(得分:0)

您需要使用@Entity注释DataProvider,使用@Id

注释id

@Entity 公共类DataProvider {    @ID    private int user_id;    @柱    private String user_name;    @柱    private String user_address;

... setter getters }

答案 1 :(得分:0)

使用Hibernate 4.x你应该使用相同的DTD 你需要添加配置文件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>