或者是Hibernate的配置文件有问题!!!!!

解决方案 »

  1.   

    这是全部照书上写的
    package events;import org.hibernate.Session;
    import util.HibernateUtil;
    import java.util.Date;public class EventManager {

    public static void main(String[] args) {


    EventManager mgr = new EventManager();

    mgr.createAndStoreEvent("My Event", new Date());

    HibernateUtil.getSessionFactory().close();
    }

    private void createAndStoreEvent(String title, Date theDate) {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    Event theEvent = new Event();
    theEvent.setTitle(title);
    theEvent.setDate(theDate);

    session.save(theEvent);

    session.getTransaction().commit();
    }


    }
    package events;import java.util.Date;public class Event{
    private Long id;

    private String title;
    private Date data;

    public Event(){
    }
    public Long getId(){
    return id;
    }
    private void setId(Long id){
    this.id =id;
    }
    public Date getDate(){
    return data;
    }
    public void setDate(Date data){
    this.data = data;
    }
    public String getTitle(){
    return title;
    }
    public void setTitle(String title){
    this.title = title;
    }
    }package events;import java.util.Date;public class Event{
    private Long id;

    private String title;
    private Date data;

    public Event(){
    }
    public Long getId(){
    return id;
    }
    private void setId(Long id){
    this.id =id;
    }
    public Date getDate(){
    return data;
    }
    public void setDate(Date data){
    this.data = data;
    }
    public String getTitle(){
    return title;
    }
    public void setTitle(String title){
    this.title = title;
    }
    }//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>        <!-- Database connection settings -->
            <property name="hibernate.connection.driver_class">org.git.mm.mysqlDriver</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">123456</property>        <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>        <!-- SQL dialect -->
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>        <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>        <!-- Disable the second-level cache  -->
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>        <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>        <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">create</property>        <mapping resource="events/Event.hbm.xml"/>    </session-factory></hibernate-configuration>
    //Event.hbm.xml
    <?xml version = "1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
               "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
               "http://hibernate.sourcefore.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
       <class name="events.Event" table="events">
          <id name="id" column="id">
              <generator class="native"/>
          </id>
          <property name="data" type="timestamp" column="data"/>
          <property name="title"/>
       </class>
    </hibernate-mapping>
      

  2.   

    数据库
    +-------+-------------+------+-----+-------------------+----------------+
    | Field | Type        | Null | Key | Default           | Extra          |
    +-------+-------------+------+-----+-------------------+----------------+
    | id    | int(11)     | NO   | PRI | NULL              | auto_increment |
    | data  | timestamp   | YES  |     | CURRENT_TIMESTAMP |                |
    | title | varchar(50) | YES  |     | NULL              |                |
    +-------+-------------+------+-----+-------------------+----------------+
      

  3.   

    Could not read mappings from resource: events/Event.hbm.xml报错信息不是很明白了吗?
      

  4.   

    按照错误提示,应该是Event.hbm.xml没有配置不对,但是我找不出来
      

  5.   

    难道没会Hibernate的人没有写过这个例子吗
      

  6.   

    不是说写的不对,你再仔细看下编译出来的包里有没有这个hbm.xml文件看开头的几段,cfg.xml已经找到了,出错明显是找不到这个hbm文件引起的.
      

  7.   

    应该是你的网络设置了代理所以dtd产生路径的问题你可以试着把hibernate-mapping-3.0.dtd 文件拷到event的同级目录然后hbm的声明这样改<!DOCTYPE hibernate-mapping PUBLIC
               "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
               "hibernate-mapping-3.0.dtd">
      

  8.   

    看看hbm文件路径正不正确events/Event.hbm.xml"。
      

  9.   

    如果找不到hbm文件 会报找不到文件的错误
      

  10.   

    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">应该是你的这里有问题! 你看下 http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd 在网页中能不能打开
      

  11.   

    http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd
    这个在网页中能打开的
      

  12.   

    我开始的时候是用ant,就是它给的不行,我又用eclipse写也不行啊
      

  13.   

    但是我后来看了一下深入浅出Hibernate的例子可以运行的啊