1

我正在尝试一个简单的教程。 链接到教程

我已经按照那里写的所有步骤进行了操作,但仍然无法正常工作。这就是我得到的: 在此处输入图像描述

这是我的主要内容:

   /*
 * 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>

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

4

1 回答 1

2

删除以下元素:

<mapping/>

这会导致您的日志中出现异常:

<mapping>配置中的元素未指定属性

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

  • 注释更容易
  • 注释是映射实体的标准 JPA 方式
于 2012-12-19T22:18:26.313 回答