元素类型“id”的内容必须匹配 - hbm xml
Posted
技术标签:
【中文标题】元素类型“id”的内容必须匹配 - hbm xml【英文标题】:The content of element type "id" must match - hbm xml 【发布时间】:2015-06-26 02:22:12 【问题描述】:我知道这个问题经常被问到。
我收到以下映射错误。
我尝试了一些排列和组合。 我已经尝试了一些解决方案,但似乎无法解决这个问题。
有人可以看看我下面的配置吗?
xmls 是有效的。 我使用hibernate 3和mysql作为数据库
错误日志:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/sample/Employee.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.sample.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:17)
at com.sample.TestEmployee.main(TestEmployee.java:13)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/sample/Employee.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:523)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1511)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1479)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1458)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1432)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1352)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1338)
at com.sample.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:12)
... 1 more
Caused by: org.hibernate.MappingException: invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:463)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:520)
... 8 more
Caused by: org.xml.sax.SAXParseException: The content of element type "id" must match "(meta*,column*,type?,generator?)".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(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.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.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.addInputStream(Configuration.java:460)
... 9 more
Employee.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "//Hibernate/Hibernate Mapping DTD 3.0//EN/" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.sample.Employee" table="employee">
<id name="idEmployee" column="ID">
<column name="uid" not-null="true"/>
<generator class="native" />
</id>
<property name="empName">
<column name="empName" length="16" not-null="true" />
</property>
<property name="sex">
<column name="sex" length="16" not-null="true" />
</property>
</class>
</hibernate-mapping>
Employee class:
package com.sample;
public class Employee
private Long idEmployee;
private String empName;
private String sex;
public Long getIdEmployee()
return idEmployee;
public void setIdEmployee(Long idEmployee)
this.idEmployee = idEmployee;
public String getEmpName()
return empName;
private String address;
private int age;
public void setEmpName(String empName)
this.empName = empName;
public String getSex()
return sex;
public void setSex(String sex)
this.sex = sex;
public String getAddress()
return address;
public void setAddress(String address)
this.address = address;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
CREATE TABLE `employee` (
`idEmployee` int(11) NOT NULL AUTO_INCREMENT,
`empName` varchar(45) NOT NULL,
`sex` varchar(45) NOT NULL,
`age` int(11) DEFAULT NULL,
`address` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idEmployee`)
);
【问题讨论】:
不相关,但请找到另一个教程来学习 Hibernate。您正在像 10 多年前一样使用它,那时还没有注释。如今,注释用于定义映射。 XML 已过时。 当然。将检查最新的教程 :) 嘿,现在我知道以前是怎么做的 ;) 谢谢!!! 【参考方案1】:我认为您错过了属性“地址”和“年龄”的映射。 你为什么不做?并检查您使用的长度标签,为什么它们都等于 16? 试试这个:
<hibernate-mapping>
<class name="com.sample.Employee" table="employee">
<id name="idEmployee" column="ID">
<generator class="native" />
</id>
<property name="empName" column="empName"></property>
<property name="sex" column="sex"></property>
<property name="address" column="address"></property>
<property name="age" column="age"></property>
</class>
</hibernate-mapping>
【讨论】:
【参考方案2】:异常说:
Caused by: org.xml.sax.SAXParseException: The content of element type "id" must match "(meta*,column*,type?,generator?)".
您的 ID 类型不匹配。在Employee
类上,您的idEmployee
变量类型为Long
,在.hbm.xml 文件中,您的ID 属性映射具有native
生成器,这意味着ID 默认为Integer
类型。所以你应该在你的 ID 属性上使用 type="long"
注释。或者只是尝试使用不同类型的 ID 生成器。在 .hbm.xml 文件中的 ID 注释上,您使用两个名称作为 ID 列。您正在使用column="ID"
和column="uid"
。 .hbm.xml 文件的示例:
<hibernate-mapping>
<class name="com.sample.Employee" table="employee">
<id name="idEmployee" type="long">
<column name="uid" not-null="true"/>
<generator class="native" />
</id>
<property name="empName">
<column name="empName" length="16" not-null="true" />
</property>
<property name="sex">
<column name="sex" length="16" not-null="true" />
</property>
</class>
</hibernate-mapping>
或者尝试使用SequenceStyleGenerator
。生成器,它通过给定的序列名称为每个表创建单独的 ID 序列。生成的 id 的默认类型是Long
。 .hbm.xml 文件示例:
<hibernate-mapping>
<class name="com.sample.Employee" table="employee">
<id name="idEmployee" column="ID">
<generator class="org.hibernate.id.enhanced.SequenceStyleGenerator">
<param name="optimizer">none</param>
<param name="increment_size">1</param>
<param name="sequence_name">seq_employee_id</param>
</generator>
</id>
<property name="empName">
<column name="empName" length="16" not-null="true" />
</property>
<property name="sex">
<column name="sex" length="16" not-null="true" />
</property>
</class>
</hibernate-mapping>
希望对你有帮助...☺
【讨论】:
以上是关于元素类型“id”的内容必须匹配 - hbm xml的主要内容,如果未能解决你的问题,请参考以下文章
configuration标签报错, 元素类型为 “configuration“ 的内容必须匹配 “...
元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id*,result*,association*,collection*,discrim
元素类型为 "resultMap" 的内容必须匹配 "(constructor?,id*,result*,association*,collection*,discrim