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 {
I开发者_开发知识库nteger 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
PRIMARY KEY (classea
(id
INT(11) NOT NULL AUTO_INCREMENT,numero
VARCHAR(255) DEFAULT NULL,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.
My understanding with one-to-one is that they share identity. That is, when you create a ClasseA, the ClasseB inside of ClasseA will have the same id. There is no explicit reference between the two tables.
For sanity, I often use many-to-one, even in the one-to-one scenario, because this creates the foreign key column in the database like you are expecting.
I'm not sure about the retrieval issue you are having without a little more information about the code being used to save and load.
I think the problem is the Class B's id isn't properly associated to Class A. I assume you're associating the 2 classes by primary key. Then try this.
First, for one-to-one relationship, both classes need one-to-one mapping configured.
Add this to Class B's definition (note: The constrained attribute is important to the hibernate schema generation tool.):
<one-to-one class="ClasseA" name="ca" constrained="true" />
Second, making sure your dependent class associate its id to Class A by using
<generator class="foreign">
精彩评论