You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Lars Soltau <la...@slab.de> on 2003/04/02 17:03:17 UTC
m:n relations
I have the following problem with m:n relations:
I have a bidirectional m:n relation, like the one in the tutorial, and
when I try to store objects, the record in the intermediate table gets
added twice, resulting in a duplicate key exception.
Here is my repository definition:
<class-descriptor class="de.dmc.vms.Promotion" table="Promotion">
<field-descriptor name="id" column="id" jdbc-type="INTEGER"
primarykey="true" autoincrement="true"/>
<collection-descriptor name="clients"
element-class-ref="de.dmc.vms.Client"
collection-class="java.util.ArrayList"
indirection-table="Promotion_Client"
auto-retrieve="true" auto-update="true" auto-delete="true"
>
<fk-pointing-to-this-class column="promotionId"/>
<fk-pointing-to-element-class column="clientId"/>
</collection-descriptor>
</class-descriptor>
<class-descriptor class="de.dmc.vms.Client" table="Client">
<field-descriptor name="id" column="id" jdbc-type="INTEGER"
primarykey="true" autoincrement="true"/>
<collection-descriptor name="promotions"
element-class-ref="de.dmc.vms.Promotion"
collection-class="java.util.ArrayList"
indirection-table="Promotion_Client"
auto-retrieve="true" auto-update="true" auto-delete="true"
>
<fk-pointing-to-this-class column="clientId"/>
<fk-pointing-to-element-class column="promotionId"/>
</collection-descriptor>
</class-descriptor>
The three tables are defined pretty straightforward. Here is the code to
add new objects:
// Build some example objects.
Promotion promo1 = new Promotion("promo1");
Promotion promo2 = new Promotion("promo2");
Client client1 = new Client("client1");
Client client2 = new Client("client2");
// Connect the objects.
promo1.addClient(client1);
promo2.addClient(client1);
promo2.addClient(client2);
// Store the objects.
broker.beginTransaction();
broker.store(promo1);
broker.store(promo2);
broker.commitTransaction();
At this point I get a duplicate key exception, and tracing the SQL
statements with P6Spy I see that the same INSERT INTO promotion_client
occurs twice, with the columns switched:
INSERT INTO Promotion_Client (clientId,promotionId) VALUES ('222','442')
INSERT INTO Promotion_Client (promotionId,clientId) VALUES ('442','222')
What did I do wrong?
Thanks for your help.
Greetings
Lars Soltau