我知道经常会问这个问题。
我得到以下映射错误。
我尝试了一些排列和组合 我尝试了一些解决方案,似乎无法解决这个问题。
有人可以查看下面的配置吗?
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`)
);
答案 0 :(得分:0)
我认为你错过了属性的映射&#34;地址&#34;和&#34;年龄&#34;。
为什么你没有制作它们?并检查您使用的长度标签,为什么它们都等于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>
答案 1 :(得分:0)
例外说:
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>
我希望它可以帮助你...☺