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 に答える