我正在尝试运行hibernate应用程序,但发生异常..这是我的代码..任何建议..提前感谢
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) 在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) 在org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70) 在org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:413) 在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“:类型,”http://www.hibernate.org/xsd/orm/hbm“:generator}'是预期的。 :origin(user.hbm.xml) 在org.hibernate.boot.jaxb.internal.AbstractBinder.jaxb(AbstractBinder.java:177) 在org.hibernate.boot.jaxb.internal.MappingBinder.doBind(MappingBinder.java:61) 在org.hibernate.boot.jaxb.internal.AbstractBinder.doBind(AbstractBinder.java:102) 在org.hibernate.boot.jaxb.internal.AbstractBinder.bind(AbstractBinder.java:57) 在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”:元,“http://www.hibernate.org/xsd/orm/hbm”:列,“http://www.hibernate.org/xsd/orm/hbm”:类型,“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(Unknown Source) 在org.hibernate.boot.jaxb.internal.AbstractBinder.jaxb(AbstractBinder.java:171) ......还有14个
答案 0 :(得分:1)
你的user.hbm.xml中有一个拼写错误。你写了#34; genereator&#34;而不是&#34; generator&#34;。修复它,它应该工作。