-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Composite Key Mapping Fails -- Entity Classes
PostPosted: Sat Dec 03, 2011 6:34 am 
Newbie

Joined: Sat Dec 03, 2011 5:43 am
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.