运行首个Hibernate程序遇hibernate.cfg.xml解析错误求助
Hey there! Let's get your first Hibernate program up and running by fixing this XML parsing issue. The error you're seeing—"Content is not allowed in trailing section"—means there's extra content (like stray characters, spaces, or even invisible whitespace) after the closing root tag of your hibernate.cfg.xml file.
Pinpointing the exact issue
Looking at your provided config code, there's a stray backslash \ right after the closing </hibernate-configuration> tag. That's the main culprit here—XML doesn't allow any content outside the root element, so this extra character is breaking the parser.
Step 1: Clean up your hibernate.cfg.xml
Remove that extra \ entirely, and make sure there's nothing else (no spaces, newlines, or hidden special characters) after the </hibernate-configuration> closing tag. Here's the corrected version of your configuration file:
<?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="hbm2ddl.auto">update</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="connection.url">jdbc:oracle:thin:@ACER:1521:XE</property> <property name="connection.username">system</property> <property name="connection.password">SHREE</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <mapping resource="employee.hbm.xml"/> </session-factory> </hibernate-configuration>
Quick additional checks
- Double-check that your
employee.hbm.xmlfile is also well-formed (no trailing content there either) and placed in the correct directory so Hibernate can locate it. - Ensure your XML files are saved with proper UTF-8 encoding—sometimes copy-pasting can introduce hidden special characters that break parsing.
Once you make these changes, try running your program again—the XML parsing error should be resolved.
内容的提问来源于stack exchange,提问作者Nam S

