[英]In Hibernate,I am getting following error at runtime
In Hibernate,I am getting following error at runtime 在Hibernate中,我在运行时遇到以下错误
1. employee.hbm.xml 1. employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package ="com.hibernate.basic">
<class name="Employee" table="Employee" lazy="false">
<id name="id" column="EMPID" type ="int">
<generator class="increment"></generator>
</id>
<property name="firstName" column="NAME"></property>
<property name="lastName"column="LNAME"></property>
</class>
2. hibernate.cfg.xml 2. hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@172.16.3.94:1521:EAMABP</property>
<property name="connection.username">EAM</property>
<property name="connection.password">EAM</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
3. Employee.java 3. Employee.java
package com.hibernate.basic;
public class Employee {
private int id;
private String FName, LName;
public String getFName() {
return FName;
}
public void setFName(String FName) {
this.FName = FName;
}
public String getLName() {
return LName;
}
public void setLName(String LName) {
this.LName = LName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
4. StoreData.java 4. StoreData.java
package com.hibernate.basic;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class StoreData {
public static void main(String[] args) {
// creating configuration object
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");// populates the data of the
// configuration file
// creating seession factory object
SessionFactory factory = cfg.buildSessionFactory();
// creating session object
Session session = factory.openSession();
// creating transaction object
Transaction t = session.beginTransaction();
Employee e1 = new Employee();
e1.setId(115);
e1.setFName("sonoo");
e1.setLName("jaiswal");
session.persist(e1);// persisting the object
t.commit();// transaction is commited
session.close();
System.out.println("successfully saved");
}
}
Error occurred after execution: 执行后发生错误:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN找不到logger(org.hibernate.cfg.Environment)的appender。 log4j:WARN Please initialize the log4j system properly. log4j:WARN请正确初始化log4j系统。 Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource employee.hbm.xml at org.hibernate.cfg.Configuration.addResource(Configuration.java:569) at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587) at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508) at org.hibernate.cfg.Configuration.configure(Configuration.java:1428) at com.hibernate.basic.StoreData.main(StoreData.java:13) Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508) at org.hibernate.cfg.Configuration.addResource(Configuration.java:566) ... 6 more Caused by: org.dom4j.DocumentException: Error on line 14 of document : Element type "property" must be followed 线程“main”org.hibernate.InvalidMappingException中的异常:无法在org.hibernate.cfg.Configuration的org.hibernate.cfg.Configuration.addResource(Configuration.java:569)中解析资源employee.hbm.xml中的映射文档。 parseMappingElement(Configuration.java:1587)atg.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)atg.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)atg.hibernate.cfg.Configuration位于org.hibernate.cfg.Configuration.configure(Configuration.java:1428)的.doConfigure(Configuration.java:1508)位于com.hibernate.basic.StoreData.main(StoreData.java:13)引起:org.hibernate。 InvalidMappingException:无法在org.hibernate.cfg.Configuration.addResource(Configuration.java:566)的org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508)中解析来自输入流的映射文档... 6更多引起by:org.dom4j.DocumentException:文档第14行出错:必须遵循元素类型“property” by either attribute specifications, ">" or "/>". 通过属性规范,“>”或“/>”。 Nested exception: Element type "property" must be followed by either attribute specifications, ">" or "/>". 嵌套异常:元素类型“property”必须后跟属性规范,“>”或“/>”。 at org.dom4j.io.SAXReader.read(SAXReader.java:482) at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499) ... 7 more org.dom4j.io.SAXReader.read(SAXReader.java:482)org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)... 7更多
name="lastName"
和column
之间缺少空格。
The error clearly states the line number and the error.Having a look at this could help. 该错误清楚地说明了行号和错误。看看这可能会有所帮助。
Error on line 14 of document : Element type "property" must be followed by either attribute specifications, ">" or "/>". 文档第14行出错:元素类型“属性”必须后跟属性规范,“>”或“/>”。 Nested exception: Element type "property" must be followed by either attribute specifications, ">" or "/>". 嵌套异常:元素类型“property”必须后跟属性规范,“>”或“/>”。
This error usually occurs because your tags are not closed.Of what you have posted here,I guess you have not closed the hibernate-mapping tag in employee.hbm.Try doing that! 这个错误通常会发生,因为你的标签没有关闭。你在这里发布的内容,我猜你还没有关闭employee.hbm中的hibernate-mapping标签。试着这样做! Hope this helps! 希望这可以帮助!
I had the same error: 我有同样的错误:
Element type "property" must be followed by either attribute specifications, ">" or "/>". 元素类型“property”必须后跟属性规范,“>”或“/>”。 Nested exception: Element type "property" must be followed by either attribute specifications, ">" or "/>". 嵌套异常:元素类型“property”必须后跟属性规范,“>”或“/>”。
And i fixed it changing a line in a *.hbm.xml file 我修改了它改变* .hbm.xml文件中的一行
There was a space missing between column="ESAMBIGUA" and length="1"/> column =“ESAMBIGUA”和length =“1”/>之间缺少空格
The first comment, the one from Eduard Wirch really helped me. 第一个评论,来自Eduard Wirch的评论真的帮助了我。 Thanks! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.