2015-05-29 81 views
1

HI我正在使用Hibernate在eclipse中開發項目。我發現這個錯誤在我的hibernate.cfg.xml文件中使用Hibernate向MySQL插入數據時出錯Error

請幫助解決這個問題。

我已經包含了所有必需的庫。

的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 
+1

你有沒有結束標記'相同的DTD <冬眠配置>'? – skozlov

+0

是否有一個原因,你使用XML配置,而不是註釋? – 2015-05-29 12:46:17

+0

是兄弟我已關閉hibernate-configration文件 –

回答

0

你需要註釋的DataProvider爲@Entity和@Id與

@Entity 公共類的DataProvider { @Id 的ID private int user_id; @Column private String user_name; @Column private String user_address;

...制定者干將 }

+1

+ Abhijit的回答 – aurelius

0

使用Hibernate 4.x的,你應該使用 你需要在配置文件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>