这是一个测试类
import org.hibernate.*;
import org.hibernate.cfg.*;public class Test 
{
public static void main(String[] args) 
   {
try 
{
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
for (int i = 0; i <200; i++) 
{
Customer customer = new Customer();
customer.setUsername("customer" + i);
customer.setPassword("customer");
session.save(customer);
}
tx.commit();
session.close();
}
catch (HibernateException e)
{
e.printStackTrace();
}
   }
}
该类编译的结果和上一类编译的结果在同一目录src下(即有d:\test\src\test.class和d:\test\src\Customer .class)按例子上所述我将hibernate.jar和hibernate包下的lib包下的所有文件都放到了d:\test\lib下,在d:\test\db下我放了sqlserver的jdbc驱动文件

解决方案 »

  1.   

    然后我再src下放了配置文件Customer.hbm.xml
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
    <class name="Customer" table="CUSTOMER">
    <id name="id" column="CID">
    <generator class="increment" />
    </id>
    <property name="username" column="USERNAME" />
    <property name="password" column="PASSWORD" />
    </class>
    </hibernate-mapping>
    和和hibernate.cfg.xml
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory name="java:/hibernate/HibernateFactory"><property name="show_sql">true</property>
    <property name="connection.driver_class">
    com.microsoft.jdbc.sqlserver.SQLServerDriver <!-- &Otilde;&acirc;&Agrave;&iuml;&Ecirc;&Ccedil;Sqlserver&micro;&Auml;JDBC driver class&Atilde;&ucirc; -->
    </property>
    <property name="connection.url">
    jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=hibernate_test;SelectMethod=cursor <!-- &Otilde;&acirc;&Agrave;&iuml;&Ecirc;&Ccedil;Sqlserver&micro;&Auml;hibernate_test&Ecirc;&yacute;&frac34;&Yacute;&iquest;&acirc;URL -->
    </property>
    <property name="connection.username">
    sa
    </property>
    <property name="connection.password">
    ssssss
    </property>
    <property name="dialect">
    org.hibernate.dialect.SQLServerDialect <!-- &Otilde;&acirc;&Agrave;&iuml;&Ecirc;&Ccedil;sqlserver&micro;&Auml;Dialect -->
    </property>
    <mapping resource="Customer.hbm.xml"/><-- &Ouml;&cedil;&para;¨Customer&micro;&Auml;&Oacute;&sup3;&Eacute;&auml;&Icirc;&Auml;&frac14;&thorn; -->
    </session-factory>
    </hibernate-configuration>
      

  2.   

    运行Test,结果为
    log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment
    ).
    log4j:WARN Please initialize the log4j system properly.
    org.hibernate.HibernateException: problem parsing configuration/hibernate.cfg.xm
    l
            at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1279)
            at org.hibernate.cfg.Configuration.configure(Configuration.java:1218)
            at org.hibernate.cfg.Configuration.configure(Configuration.java:1204)
            at Test.main(Test.java:10)
    Caused by: org.dom4j.DocumentException: Error on line 26 of document  : The cont
    ent of elements must consist of well-formed character data or up. Nested exc
    eption: The content of elements must consist of well-formed character data or ma
    rkup.
            at org.dom4j.io.SAXReader.read(SAXReader.java:355)
            at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1274)
            ... 3 more我很痛苦,这个例子折腾了好长时间了,到底哪错了?
      

  3.   

    好像在说你的hibernate.cfg.xml文件的格式有问题
    贴上来看看
      

  4.   

    使用dom4j解析你的hibernate.cfg.xml文件时出错,Error on line 26 of document  在你的26行,The content of elements must consist of well-formed character data or up.
      

  5.   

    problem parsing configuration/hibernate.cfg.xm
    把.class目录下的java文件去掉试试,或者是.class 的目录不对
      

  6.   

    这是我的hibernate.cfg.xml<?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory name="java:/hibernate/HibernateFactory"><property name="show_sql">true</property>
    <property name="connection.driver_class">
    com.microsoft.jdbc.sqlserver.SQLServerDriver <!-- &Otilde;&acirc;&Agrave;&iuml;&Ecirc;&Ccedil;Sqlserver&micro;&Auml;JDBC driver class&Atilde;&ucirc; -->
    </property>
    <property name="connection.url">
    jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=hibernate_test;SelectMethod=cursor <!-- &Otilde;&acirc;&Agrave;&iuml;&Ecirc;&Ccedil;Sqlserver&micro;&Auml;hibernate_test&Ecirc;&yacute;&frac34;&Yacute;&iquest;&acirc;URL -->
    </property>
    <property name="connection.username">
    sa
    </property>
    <property name="connection.password">
    ssssss
    </property>
    <property name="dialect">
    org.hibernate.dialect.SQLServerDialect <!-- &Otilde;&acirc;&Agrave;&iuml;&Ecirc;&Ccedil;sqlserver&micro;&Auml;Dialect -->
    </property>
    <mapping resource="Customer.hbm.xml"/><-- &Ouml;&cedil;&para;¨Customer&micro;&Auml;&Oacute;&sup3;&Eacute;&auml;&Icirc;&Auml;&frac14;&thorn; -->
    </session-factory>
    </hibernate-configuration>
      

  7.   

    在hibernate.cfg.xml中<mapping resource="Customer.hbm.xml"/〉老是报我这里有错误,我连相应的dtd都看了,就应该是这样写才对呀?