Hi all, i want use hibernate for a java swing app develops with
netbeans.
My problem is the mapping of entity class, here my steps:
1- New project using Java Application
2- Create a
hibernate.cfg.xml from "new file -> Hibernate -> Hibernate Configuration Wizard"
hibernate.cfg.xml is like this:
Code:
<?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.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:tcp://localhost/my_db_path</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="Hibernate/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3- Adding some props to cfg file: hibernate.hbm2ddl.auto=update and hibernate.show_slq=true
4- Create
HibernateUtil.java from "new file -> Hibernate -> HibernateUtil.java"
5- Create a class that realize an object Person from "new file -> Persistance -> Entity Class"
Person.java is like this:
Code:
package Hibernate;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Person implements Serializable {
private static final long serialVersionUID=1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
public Person() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
@Override
public int hashCode() {
int hash=0;
hash+=(id!=null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if(!(object instanceof Person)) {
return false;
}
Person other=(Person)object;
if((this.id==null&&other.id!=null)||(this.id!=null&&!this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Hibernate.Person[id="+id+"]";
}
}
5- Create
Person.hbm.xml from "new file -> Hibernate -> Hibernate Mapping Wizard" and choose the Person class and NO database table (there aren't tables in db yet).
Person.hbm.xml is like this:
Code:
<?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 dynamic-insert="false" dynamic-update="false" mutable="true" name="Hibernate.Person" optimistic-lock="version" polymorphism="implicit" select-before-update="false"/>
</hibernate-mapping>
I think this mapping don't work, what is my wrong step?
Im using netbeans 6.8 with hibernate plugin.
Thanks.