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)
You tell Hibernate to load the mapping from the hibernate.cfg.xml file:
But you also tell it to add the Person class as a mapped-trough annotations entity:
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.