java—由以下原因引起:org.xml.sax.saxparseexception;行号:5;列数:42;元素类型“id”的内容不完整,

wlsrxk51  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(270)

我刚刚开始使用hibernate,我一直在尝试解决这个特定的错误,但不知道为什么会发生这种情况?
这是我收到的错误信息。

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.MappingException: Error reading resource: com/sood/hibernate/student.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:424)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1137)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1093)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1021)
    at com.sood.hibernate.SimpleTest.main(SimpleTest.java:14)
Caused by: org.hibernate.MappingException: invalid mapping
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:372)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:421)
    ... 4 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 42; The content of element type "id" is incomplete, it must match "(meta*,column*,generator)".
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
    at org.apache.xerces.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:371)
    ... 5 more

这可能是因为我的xmlMap

<?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>
    <class name="com.sood.hibernate.Student" table="STUDENT">
        <id name="ID" column="id" type="long" />
        <property column="STUDENT_NAME" name="name" type="string" />
        <property column="DEGREE" name="degree" type="string" />
        <property column="ROLL" name="roll" type="string" />
        <property column="PHONE" name="phone" type="string" />
    </class>
</hibernate-mapping>

这是我的hibernate配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/aliens</property>
        <property name="hibernate.connection.username">sood</property>
        <property name="hibernate.connection.password">sood</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping resource="com/sood/hibernate/student.hbm.xml" />
    </session-factory>
</hibernate-configuration>

如何做到这一点?
编辑01:
添加Map文件:

package com.sood.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class SimpleTest {

    public static void main(String[] args) {

        Configuration cfg = new Configuration();
        cfg.configure("/com/sood/hibernate/hibernate.cfg.xml");

        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();
        Student student = new Student();
        student.setName("student01");
        student.setRoll("101");
        student.setPhone("12345");
        student.setDegree("BE");

        Transaction tx = session.beginTransaction();
        session.save(student);
        System.out.println("Object saved successfully.....!!");
        tx.commit();
        session.close();
        factory.close();
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题