Hi,
currently I try to get a many-to-many mapping to work. I don't understand how to use the entity classes generated by Hibernate in this particular case.
A separate java class for the composite primary key of BondAtom is created, but I don't know how to associate the class with the BondAtom class.
My database contains three tables: Bond <-> BondAtom <-> Atom
Every atom has many bonds and every bond has many atoms.
The Atom.xml snippet (AtomId is the primary key in ATOM)
Code:
<set name="bondAtomsByAtomId" inverse="true" cascade="all">
<key>
<column name="atom_id" not-null="true"/>
</key>
<one-to-many not-found="ignore" class="uk.ac.ebi.spectradb.map.BondAtom"/>
</set>
The Bond.xml snippet (BondId is the primary key in BOND)
Code:
<set name="bondAtomsByBondId" inverse="true">
<key>
<column name="bond_id" not-null="true"/>
</key>
<one-to-many not-found="ignore" class="uk.ac.ebi.spectradb.map.BondAtom"/>
</set>
BondAtom.xml
Code:
<class name="uk.ac.ebi.spectradb.map.BondAtom" table="BOND_ATOM" schema="" catalog="spectradb">
<composite-id mapped="true" class="uk.ac.ebi.spectradb.map.BondAtomPK">
<key-property name="bondId" column="bond_id"/>
<key-property name="atomId" column="atom_id"/>
</composite-id>
<property name="orderNumber" column="order_number"/>
<many-to-one name="bondByBondId" class="uk.ac.ebi.spectradb.map.Bond">
<column name="bond_id" not-null="true"/>
</many-to-one>
<many-to-one name="atomByAtomId" class="uk.ac.ebi.spectradb.map.Atom">
<column name="atom_id" not-null="true"/>
</many-to-one>
</class>
The class for the composite primary key
Code:
public class BondAtomPK implements Serializable {
private int bondId;
public int getBondId() {
return bondId;
}
public void setBondId(int bondId) {
this.bondId = bondId;
}
private int atomId;
public int getAtomId() {
return atomId;
}
public void setAtomId(int atomId) {
this.atomId = atomId;
}
}
The actual BondAtom class
Code:
public class BondAtom {
private int bondId;
public int getBondId() {
return bondId;
}
public void setBondId(int bondId) {
this.bondId = bondId;
}
private int atomId;
public int getAtomId() {
return atomId;
}
public void setAtomId(int atomId) {
this.atomId = atomId;
}
private int orderNumber;
public int getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(int orderNumber) {
this.orderNumber = orderNumber;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
BondAtom bondAtom = (BondAtom) o;
if (atomId != bondAtom.atomId)
return false;
if (bondId != bondAtom.bondId)
return false;
if (orderNumber != bondAtom.orderNumber)
return false;
return true;
}
@Override
public int hashCode() {
int result = bondId;
result = 31 * result + atomId;
result = 31 * result + orderNumber;
return result;
}
private Bond bondByBondId;
public Bond getBondByBondId() {
return bondByBondId;
}
public void setBondByBondId(Bond bondByBondId) {
this.bondByBondId = bondByBondId;
}
private Atom atomByAtomId;
public Atom getAtomByAtomId() {
return atomByAtomId;
}
public void setAtomByAtomId(Atom atomByAtomId) {
this.atomByAtomId = atomByAtomId;
}
}
In short, I am looking for an example of how to use this constellation of classes in a way that my hibernate session does not complain about missing foreign key references or parameter indices out of range. "Before" (different IDE, Hibernate version) I could simply explicitly add the primary key object to my bondAtom object via a setter method.
Any help would be much appreciated.
Thank you,
Stephan