1

I was trying a simple tutorial. Link to tutorial

I have followed all of the steps as it was written there but still it doesn't work. And this is what i get : enter image description here

This is my main:

   /*
 * 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();
        }
    }
}

This my 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>

And my mapping:

<?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>

What is wrong here ? Why won't it populate my database ?

1 Answer 1

2

Remove the following element:

<mapping/>

which causes the exception in your logs:

<mapping> element in configuration specifies no attribute

I would try to find another tutorial, showing how to use annotations for mapping instead of XML files, because

  • annotations are much easier
  • annotations are the standard JPA way of mapping entities
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.