Hibernate + Mysql没有数据库填充

时间:2012-12-19 22:07:44

标签: hibernate

我正在尝试一个简单的教程。 Link to tutorial

我已按照所有步骤编写,但仍无法正常工作。 这就是我得到的:  enter image description here

这是我的主要内容:

   /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernatetest;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author ktelfon
 */
public class HibernateTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Session session = null;
        try {
            SessionFactory sessionFactory = new org.hibernate.cfg.Configuration().configure("hibernatetest/hibernate.cfg.xml").buildSessionFactory();
            session = sessionFactory.openSession();
            session.beginTransaction();

            System.out.println("Populating the database !");
            Customer customer = new Customer();
            customer.setCustomerName("Chathura");
            customer.setCustomerAddress("221B,Moratuwa");
            customer.setCustomerEmail("priyankarahac@gmail.com");

            session.save(customer);
            session.getTransaction().commit();

            System.out.println("Done!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            session.flush();
            session.close();
        }
    }
}

这是我的cfg:

<?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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/retailer?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="show_sql">true</property>
    <mapping resource="hibernatetest/customersmapping.hbm.xml"/>
    <mapping/>
  </session-factory>
</hibernate-configuration>

我的映射:

<?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="hibernatetest.Customer" table="customers">
  <id column="C_ID" name="customerID" type="int">
  <generator class="native">
  </generator></id>
  <property name="customerName">
  <column name="name">
  </column></property>
  <property name="customerAddress">
  <column name="address">
  </column></property>
  <property name="customerEmail">
  <column name="email">
  </column></property>
  </class>
</hibernate-mapping>

这里有什么问题?为什么不填充我的数据库?

1 个答案:

答案 0 :(得分:2)

删除以下元素:

<mapping/>

会导致日志中出现异常:

  配置中的

<mapping>元素指定无属性

我会尝试找另一个教程,展示如何使用注释来映射而不是XML文件,因为

  • 注释更容易
  • 注释是映射实体的标准JPA方式