Hi to all !!

First of all I would like to thank the authors of Hibernate for a great
piece of work.
It really works !!!

I have already successfuly recommended Hibernate to couple of my colleagues.
So the hibernated community is growing ... :)






My question concerns best practices of using composite ID's in collection
elements.

The use case is the following:

Stuff contains a set of StuffUnitFactors. It's a one-to-many relationship:


StuffUnitFactor refers back to Stuff, refers to Unit and contains aditional
property (bidirectional one-to-many)
 - property: double factor
 - many-to-one Unit
 - many-to-one Stuff

I have also overriden the hashCode() and equals() methods on StuffUnitFactor
so that they are equal when stuff && unit are equal


In my code I am using the recommended patern for bi-directional
parent-children relationship:


StuffUnitFactor child= new StuffUnitFactor();
Stuff parent= session.load(parentId);
parent.add(child);
session.save(child);

And then I have found out that it's possible to insert two child elements
into database which are equal !!!





QUESTION:

How can I assure the uniquness of StuffUnitFactor in database ??

Of course the best solution would be if I have a primary key on
StuffUnitFactor like: primary_key(stuff, unit)


I have already tried to use what you recommend:

    <composite-element class="warehouse.persistent.StuffUnitFactor">

        <property  name="factor"  type="double"  update="true" insert="true"
column="factor" not-null="true" />

        <many-to-one name="parent" class="warehouse.persistent.Stuff"
cascade="none" outer-join="auto" update="true" insert="true" column="parent"
not-null="true"  />

        <many-to-one  name="unit" class="warehouse.persistent.Unit"
cascade="none"  outer-join="auto" update="true"  insert="true" column="unit"
not-null="true"   />

    </composite-element>


But it explicitly makes primary_key(stuff, unit, factor) which is completly
useless as I want to have unique stuff-unit pair.



Then I tried to make o composite ID on StuffUnitFactor itself - but I could
not get it working as collection element ....


Any ideas, practices ??






My Mapping files are like:



    <class
        name="warehouse.persistent.Stuff"
        table="stuff"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <set
            name="stuffUnitFactors"
            lazy="true"
            inverse="true"
            cascade="all-delete-orphan"
            sort="unsorted"
        >

              <key
                  column="parent"
              />

              <one-to-many
                  class="warehouse.persistent.StuffUnitFactor"
              />
        </set>



    </class>


    <class
        name="warehouse.persistent.StuffUnitFactor"
        table="stuffunitfactor"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Long"
        >
            <generator class="sequence">
            </generator>
        </id>

        <property
            name="factor"
            type="double"
            update="true"
            insert="true"
            column="factor"
            not-null="true"
        />

        <many-to-one
            name="parent"
            class="warehouse.persistent.Stuff"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="parent"
            not-null="true"
        />

        <many-to-one
            name="unit"
            class="warehouse.persistent.Unit"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="unit"
            not-null="true"
        />

    </class>


thanks for your help

Wojtek Bentkowski




-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to