0
votes

I'm new to the hibernate and eclipse. I successfully did it with Derby by watching a youtube video. Now I want to do it with MySql.

I was successful to connect MySQL to eclips

Next i wrote simple persistant class person

import javax.persistence.Entity; import javax.persistence.Id;

@Entity public class Person {

private String name;
private int id;


public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@Id
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

}


Next I wrote a test class for that

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;


public class PersonTest {

    /**
     * @param args
     */
    public static void main(String[] args) {

        AnnotationConfiguration cfg = new AnnotationConfiguration();
        cfg.addAnnotatedClass(Person.class);
        cfg.configure("hibernate.cfg.xml");

        new SchemaExport(cfg).create(true, true);

        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();

        Person person = new Person();

        person.setName("Alex");
        person.setId(1);

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

    }

}

My Hibernate configuration file

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

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/ruwaKuppi</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1234</property>
        <property name="hibernate.default_schema">ruwaKuppi</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping resource="person.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

My Mapping file

<?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="Person" table="PERSON">
   <id name="id" type="int" column="ID" >
   <generator class="assigned"/>
  </id>

  <property name="name">
   <column name="NAME" />
  </property>

 </class>
</hibernate-mapping>

Now I'm getting this error.... ![enter image description here][2]

13:13:17,795 INFO Version:15 - Hibernate Annotations 3.4.0.GA 13:13:17,818 INFO Environment:560 - Hibernate 3.3.2.GA 13:13:17,821 INFO Environment:593 - hibernate.properties not found 13:13:17,825 INFO Environment:771 - Bytecode provider name : javassist 13:13:17,830 INFO Environment:652 - using JDK 1.4 java.sql.Timestamp handling 13:13:17,927 INFO Version:14 - Hibernate Commons Annotations 3.1.0.GA 13:13:17,949 INFO Configuration:1474 - configuring from resource: hibernate.cfg.xml 13:13:17,950 INFO Configuration:1451 - Configuration resource: hibernate.cfg.xml 13:13:18,059 INFO Configuration:600 - Reading mappings from resource : person.hbm.xml 13:13:18,147 INFO Configuration:1589 - Configured SessionFactory: null 13:13:18,173 INFO Dialect:175 - Using dialect: org.hibernate.dialect.MySQLDialect 13:13:18,307 INFO HbmBinder:322 - Mapping class: Person -> PERSON 13:13:18,326 INFO AnnotationBinder:419 - Binding entity from annotated class: Person 13:13:18,364 INFO Mappings:161 - duplicate import: Person->Person 13:13:18,367 INFO EntityBinder:422 - Bind entity Person on table Person Exception in thread "main" org.hibernate.DuplicateMappingException: Duplicate class/entity mapping Person at org.hibernate.cfg.Mappings.addClass(Mappings.java:141) at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:789) at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546) at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291) at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:803) at org.hibernate.tool.hbm2ddl.SchemaExport.(SchemaExport.java:128) at org.hibernate.tool.hbm2ddl.SchemaExport.(SchemaExport.java:91) at PersonTest.main(PersonTest.java:18)

Please can any one help me to get out from this mess... Thank you very much for your concern..

p.s :I know that last code is not clear....I think this is the important line of above

13:13:18,367 INFO EntityBinder:422 - Bind entity Person on table Person Exception in thread "main" org.hibernate.DuplicateMappingException: Duplicate class/entity mapping Person at org.hibernate.cfg.Mappings.addClass(Mappings.java:141)

3
Dear JB NIzet <br/> I removed it and I got new error,(I previously met with this error before I insert the person.hbm.xml file) now I'm getting it back <br/> 14:33:23,608 INFO SessionFactoryObjectFactory:105 - Not binding factory to JNDI, no JNDI name configured 14:33:23,612 INFO SchemaUpdate:155 - Running hbm2ddl schema update Exception in thread "main" org.hibernate.HibernateException:No 'CurrentSessionContext configured! at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:620)at PersonTest.main(PersonTest.java:21) What should I do now..Thank you in advance - Ruwantha
Add <property name="current_session_context_class">thread</property> to your XML config file, and read docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/… to understand what it means. - JB Nizet

3 Answers

1
votes

You tell Hibernate to load the mapping from the hibernate.cfg.xml file:

cfg.configure("hibernate.cfg.xml");

But you also tell it to add the Person class as a mapped-trough annotations entity:

cfg.addAnnotatedClass(Person.class);

Decide if you want to map the Person entity using annotations or using XML. If you choose XML, then remove the cfg.addAnnotatedClass(Person.class); line. If you choose annotations, then remove the <class name="Person" table="PERSON"> (and all its sub-elements of course) from the XML file.

1
votes

person class file

@Entity public class Person {

private String name;
@Id
private int id;



public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

}

test class public class PersonTest {

/**
 * @param args
 */
public static void main(String[] args) {
 Person person = new Person();

    person.setName("Alex");
    person.setId(1);

    SessionFactory factory =  Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();
    session.beginTransaction();
    session.save(person);
    session.getTransaction().commit();

}

}

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/ruwaKuppi</property>
    <property name="connection.username">root</property>
    <property name="connection.password">1234</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>

    <!-- Names the annotated entity class -->
    <mapping class="Put your package name and class name here"/>

</session-factory>

</hibernate-configuration>

no need to add mapping file make sure your hibernate jars are included with jdbc jar. include jar from lib-bytecode-javassist, jpa and required. check hibernate.cfg.xml file,itshould include in src folder

0
votes

Remove annotation '@id' from getter of id of Person class