Hibernate version: 3.2final
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="fr.ineo.tinea.ramses.model.persistent" auto-import="true">
<class name="RMSSite" table="sit_site">
<id name="id" type="integer" column="SIT_ID">
<generator class="increment"/>
</id>
<property name="ip" column="SIT_ip" type="string" not-null="true" length="20"/>
<property name="latitude" column="SIT_latitude" type="string" not-null="false" length="20"/>
<property name="longitude" column="SIT_longitude" type="string" not-null="false" length="20"/>
<property name="name" column="SIT_name" type="string" not-null="false" length="20"/>
<property name="description" column="SIT_description" type="string" not-null="false" length="200"/>
<property name="lastSignOfLifeDate" column="SIT_last_sign_of_life" type="java.util.Date" not-null="false"/>
<idbag name="paramValueList" table="SPA_SITE_PARAMETER">
<collection-id type="integer" column="SPA_ID">
<generator class="increment" />
</collection-id>
<key column="SIT_SITE_SIT_ID"/>
<many-to-many class="RMSParamValue" column="PVA_PARAMETER_VALUE_PVA_ID" lazy="false" fetch="join"/>
</idbag>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Use of a standard HibernateUtil class
Code:
public static Session getSession()
throws HibernateException {
Session s = (Session) threadSession.get();
try {
if (s == null) {
log.debug("Opening new Session for this thread.");
if (getInterceptor() != null) {
log.debug("Using interceptor: " + getInterceptor().getClass());
s = getSessionFactory().openSession(getInterceptor());
} else {
s = getSessionFactory().openSession();
}
threadSession.set(s);
}
} catch (HibernateException ex) {
throw new HibernateException(ex);
}
return s;
}
public static void closeSession()
throws HibernateException {
try {
Session s = (Session) threadSession.get();
threadSession.set(null);
if (s != null && s.isOpen()) {
log.debug("Closing Session of this thread.");
s.close();
}
} catch (HibernateException ex) {
throw new HibernateException(ex);
}
}
Full stack trace of any exception that occurs:No error occurs in console
Name and version of the database you are using:Mysql 5 stable latest version, installer mysql-essential-5.0.51b-win32.msi
All tables are created in MyISAM format
Java file (hashcode/equals) of Site class:Code:
private int ip;
....
@Override
public boolean equals(Object obj) {
if (!(obj instanceof RMSSite) || obj == null) return false;
else {
RMSSite site = (RMSSite)obj;
return this.getIp() == site.getIp();
}
}
@Override
public int hashCode() {
return new Integer(getId()).hashCode();
}
What happen? :Code:
Session session = RMSHibernateUtil.getSession();
RMSHibernateUtil.beginTransaction();
RMSSite s1 = new RMSSite();
s1.setIp("192.168.0.1");
s1.setDescription("description 1");
session.saveOrUpdate(s1);
session.flush();
RMSSite s2 = new RMSSite();
s2.setIp("192.168.0.1");
s1.setDescription("description 2");
//here should be the same object with the same business id (IP of a site)
session.saveOrUpdate(s2);
session.flush();
After this code (used in JUnit to validate my mapping files), i have 2 rows in database, and the two have the same value in IP field.
Why Hibernate does not assume the two object are "the same one" and do something like an update on s1, or nothing at all, but not creating a second row in DB?
What do i ignore?
I read this and i think i should be right in my use of "equals"...
http://www.hibernate.org/109.html
Thanks for helping !