I got following example:
e.g:
Code:
package com.animals;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
* @hibernate.class table = "Animal"
* polymorphism="implicit"
*/
public class Animal {
private long eom;
private String name;
/**
*
*/
public Animal() {
super();
}
/**
* @hibernate.id column = "animal_eom"
* length = "4"
* type = "long"
* generator-class = "increment"
*/
public long getEom() {
return this.eom;
}
/**
* @hibernate.property
* column = "animal_name"
* length = "45"
* not-null = "true"
* type = "string"
*/
public String getName() {
return this.name;
}
public void setEom(long eom) {
this.eom = eom;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object object) {
if (!(object instanceof Animal)) {
return false;
}
Animal rhs = (Animal) object;
return new EqualsBuilder().append(
this.eom, rhs.eom).append(this.name, rhs.name).isEquals();
}
public int hashCode() {
return new HashCodeBuilder(-526618669, 1122276377).append(
super.hashCode()).append(this.eom).append(this.name)
.toHashCode();
}
}
then:
Code:
package com.animals;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
* @hibernate.class table = "Animal"
*
*/
public class Dog extends Animal{
private String color;
/**
*
*/
public Dog() {
super();
// TODO Auto-generated constructor stub
}
/**
* @hibernate.property
* column = "animal_color"
* length = "45"
* not-null = "true"
* type = "string"
*/
public String getColor() {
return this.color;
}
public void setColor(String color) {
this.color = color;
}
public boolean equals(Object object) {
if (!(object instanceof Dog)) {
return false;
}
Dog rhs = (Dog) object;
return new EqualsBuilder().append(
this.color, rhs.color).isEquals();
}
public int hashCode() {
return new HashCodeBuilder(-900848193, 1510557323).append(
super.hashCode()).append(this.color).toHashCode();
}
}
result following files:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="com.animals.Animal"
table="Animal"
polymorphism="implicit"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="eom"
column="animal_eom"
type="long"
length="4"
>
<generator class="increment">
</generator>
</id>
<property
name="name"
type="string"
update="true"
insert="true"
access="property"
column="animal_name"
length="45"
not-null="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Animal.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
and
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="com.animals.Dog"
table="Animal"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="eom"
column="animal_eom"
type="long"
length="4"
>
<generator class="increment">
</generator>
</id>
<property
name="color"
type="string"
update="true"
insert="true"
access="property"
column="animal_name"
length="45"
not-null="true"
/>
<property
name="name"
type="string"
update="true"
insert="true"
access="property"
column="animal_name"
length="45"
not-null="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Dog.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
if i now try following on a Session, it throws me a Class Cast Exception:
Code:
//this line works
Animal a=((Animal)Session.get(Animal.class,new Long(1));
System.out.println(a);
//this not and throws exception
Dog d=((Dog)Session.get(Dog.class,new Long(1));
System.out.println(a);
I think this is because Hibernate founds in cache the first loaded Object (Animal) and return it to me, and so its not possible to cast them
to a Dog .
but doing this:
Code:
Dog d=((Dog)Session.get(Dog.class,new Long(1));
System.out.println(a);
Animal a=((Animal)Session.get(Animal.class,new Long(1));
System.out.println(a);
it prints twice the Dog Object (hashCode is equal)...
do you have any idea why this is so?