2012-12-19 178 views
1

我正在尝试一个简单的教程。 Link to tutorial休眠+ Mysql没有数据库填充

我遵循了所有步骤,因为它是在那里写的,但它仍然无效。 这是我所得到的: 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("[email protected]"); 

      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> 

是什么这里错了吗?为什么它不会填充我的数据库?

回答

2

删除以下元素:

<mapping/> 

这会导致你的日志例外:在配置

<mapping>元素指定无属性

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

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

THX了很多与此 – user1134746