I wrote these two classes:
public class ClasseA {
Integer id;
String numero;
ClasseB cb;
public ClasseB getCb() {
return cb;
}
public void setCb(ClasseB cb) {
this.cb = cb;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNumero() {
return numero;
}
public void setNumero(String numero) {
this.numero = numero;
}
}
and
public class ClasseB {
Integer id;
String annotazione;
public String getAnnotazione() {
return annotazione;
}
public void setAnnotazione(String annotazione) {
this.annotazione = annotazione;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
As you can see in ClasseA there is a reference to ClasseB.
this is the mapping:
<?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="ClasseA" optimistic-lock="version" polymorphism="implicit" select-before-update="false">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="identity"/>
</id>
<property name="numero" type="java.lang.String">
<column name="numero"/>
</property>
<one-to-one cascade="all" class="ClasseB" name="cb"/>
</class>
<class dynamic-insert="false" dynamic-update="false" mutable="true" name="ClasseB" optimistic-lock="version" polymorphism="implicit" select-before-update="false">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="identity"/>
</id>
<property name="annotazione" type="java.lang.String">
<column name="annotazione"/>
</property>
</class>
</hibernate-mapping>
Two things don't work as expected:
First of all, since I'm using hdb2ddl with update I'd expect do generate a table for classeA with a reference to classeB (and of course a table for classeB). Which isn't. All I get is:
CREATE TABLE
classea
(id
INT(11) NOT NULL AUTO_INCREMENT,numero
VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (id
) ) ENGINE=INNODB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1CREATE TABLE
classeb
(id
int(11) NOT NULL AUTO_INCREMENT,annotazione
varchar(255) DEFAULT NULL, PRIMARY KEY (id
) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1Second: If I save an istance of classeA with cb correctly set to a cb istance, it will work putting a row on the first and a row on the second table. But on retrivial it doesn't even load classeA....
Please help as I think I didn't understand properly this kind of association. Please don't suggest to use annotations as I can't. Tnx in advance.