我試圖休眠運行應用程序,但在這裏發生異常..是我的代碼..any建議..謝謝提前異常運行休眠應用
hibernate.cfg.xml
:
<?xml version='1.0' encoding='utf-8'?>
<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/mydb </property>
<property name="connection.user">root </property>
<property name="connection.password">root</property>
<!-- Related to the connection END -->
<!-- Related to hibernate properties START -->
<property name="show_sql">true</property>
<property name="dialet">org.hibernate.dialect.MYSQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<!-- Related to hibernate properties END-->
<!-- Related to mapping START-->
<mapping resource="user.hbm.xml" />
<!-- Related to the mapping END -->
</session-factory>
</hibernate-configuration>
user.hbm.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping>
<class name="DataProvider" table="user_info">
<id name="user_id" column="id">
<genereator class="assigned" />
</id>
<property name="vuser_name" column="name"/>
<property name="user_address" column="address"/>
</class>
</hibernate-mapping>
DataProvider.java
:
public class DataProvider {
private int user_id;
private String user_name;
private String user_address;
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;
}
}
DataInsertion.java
:
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("name");
provider.setUser_address("adress");
Transaction tr = (Transaction) session.beginTransaction();
session.save(provider);
System.out.println("Object Saved");
try {
tr.commit();
} catch (SecurityException | HeuristicMixedException | HeuristicRollbackException | RollbackException
| SystemException e) {
e.printStackTrace();
}
session.close();
sf.close();
}
}
和例外是
2016年3月20日下午12時49分01秒org.hibernate.annotations.common.reflection.java.JavaReflectionManager 信息:HCANN000001 :Hibernate Commons Annotations {5.0.1.Final} 2016年3月20日12:49:01 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity 警告:HHH90000012:識別過時的hibernate命名空間http://hibernate.sourceforge.net/hibernate-mapping 。改爲使用名稱空間http://www.hibernate.org/dtd/hibernate-mapping。隨時可以刪除對過時的DTD/XSD名稱空間的支持。 線程「main」中的異常org.hibernate.boot.InvalidMappingException:無法解析映射文檔:user.hbm.xml(RESOURCE) at org.hibernate.boot.jaxb.internal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:46 ) 在org.hibernate.boot.jaxb.internal.UrlXmlSource.doBind(UrlXmlSource.java:36) 在org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:59) 在org.hibernate.boot .MetadataSources.addResource(MetadataSources.java:274) at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java :413) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724) 在DataInsertion.insertInfo(DataInsertion.java:22) 在DataInsertion.main(DataInsertion.java:15) 原因:org.hibernate.boot.MappingException:無法在第9行和第32列執行反編組。消息:cvc-complex-type.2.4.a:發現無效內容從元素'genereator'開始。其中一個'{「http://www.hibernate.org/xsd/orm/hbm」:meta,「http://www.hibernate.org/xsd/orm/hbm」:列,「http://www.hibernate.org/xsd/orm/hbm」:type,「http://www.hibernate.org/xsd/orm/hbm」:generator}'。 :origin(user.hbm.xml) at org.hibernate.boot.jaxb.internal.AbstractBinder.jaxb(AbstractBinder.java:177) at org.hibernate.boot.jaxb.internal.MappingBinder.doBind(MappingBinder.java :61) at org.hibernate.boot.jaxb.internal.AbstractBinder.doBind(AbstractBinder.java:102) at org.hibernate.boot.jaxb.internal.AbstractBinder.bind(AbstractBinder.java:57) at org .hibernate.boot.jaxb.internal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:43) ... 10個以上 引起:javax.xml.bind.UnmarshalException - 帶有鏈接的異常: [org.xml.sax。的SAXParseException; lineNumber:9; columnNumber:32; cvc-complex-type.2.4.a:從元素'genereator'開始找到無效的內容。「{」http://www.hibernate.org/xsd/orm/hbm「:meta,」http://www.hibernate.org/xsd/orm/hbm「:列,」http://www.hibernate.org/xsd/orm/hbm「:type,」http://www.hibernate.org/xsd/orm/hbm「:預計發生器}'。 at com.sun.xml.internal.bind.v2.runtime.unmarshaller .UnmarshallerImpl.handleStreamException(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller .UnmarshallerImpl.unmarshal(來源不明) 在org.hibernate.boot.jaxb.internal.AbstractBinder.jaxb(AbstractBinder.java:171) ...... 14多個
在您的配置文件中有一堆拼寫錯誤, G。 'genereator'而不是'generator','dialet'而不是''dialect''或許更多。所以毫不奇怪,它無法運行。 –