0
26 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.2.GA
29 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
38 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
47 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp         handling
190 [main] INFO org.hibernate.cfg.Configuration - configuring from resource:     /hibernate.cfg.xml
191 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
419 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : com/jcz/domain/Employee.hbm.xml
489 [main] ERROR org.hibernate.util.XMLHelper - Error parsing XML: XML InputStream(7)        The markup in the document preceding the root element must be well-formed.
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.jcz.view.TestMain.addEmployee(TestMain.java:38)
at com.jcz.view.TestMain.main(TestMain.java:24)
 Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document   from  resource com/jcz/domain/Employee.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:616)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1635)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1603)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1582)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at com.jcz.util.MySessionFactory.<clinit>(MySessionFactory.java:15)
... 2 more
 Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:555)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:613)
... 9 more
 Caused by: org.dom4j.DocumentException: Error on line 7 of document  : The markup in the    document preceding the root element must be well-formed. Nested exception: The markup in   the document preceding the root element must be well-formed.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:546)
... 10 more

1.mydb2数据库中的表(MySQL)

create table employee(
id int primary key auto_increment,
name varchar(64) not null,
email varchar(64) not null);

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">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/mydb2</property>          
        <property name="connection.username">root</property>
        <property name="connection.password">85428133</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="transaction.factory_class">
        org.hibernate.transaction.JDBCTransactionFactory
        </property>
        <property name="show_sql">true</property>

        <mapping resource="com/jcz/domain/Employee.hbm.xml" />
    </session-factory>
</hibernate-configuration>

3.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.jcz.domain">
    <class name="Employee" table="employee">
        <id name="id" column="id">
            <generator class="increment"/>
        </id>

        <property name="name">
            <column name="name" length="64" not-null="true" />
        </property>
        <property name="email">
            <column name="email" length="64" not-null="true"  />
        </property>

    </class>
</hibernate-mapping>

4.Employee.java

package com.jcz.domain;

public class Employee {

    private int id;
    private String name;
    private String email;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

5.TestMain.java

package com.jcz.view;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.jcz.domain.Employee;
import com.jcz.util.MySessionFactory;

public class TestMain {

    public static void main(String[] args) {
        addEmployee();
    }

    public static void addEmployee() {

        Employee employee = new Employee();
        employee.setName("biancheng");
        employee.setEmail("biancheng@gmail.com");


        Session session = MySessionFactory.getSessionFactory().openSession();
        Transaction transaction=session.beginTransaction();

        session.save(employee);
        transaction.commit();
        session.flush();
        session.close();
    }
}

6.MySessionFactory.java

package com.jcz.util;

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

final public class MySessionFactory {

    private static SessionFactory sessionFactory=null;

    private MySessionFactory(){
    }

    static{
        sessionFactory=new Configuration().configure().buildSessionFactory();
    }

    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
}

我刚开始学习休眠,这个错误给我带来了麻烦。谁能帮我解决这个问题?

4

0 回答 0