Bruce Snyder wrote:
This one time, at band camp, Mattias Campe said:

MC>Mattias Campe wrote:
MC>> Ralf Joachim wrote:
MC>> <snip problems with many-to-many relationship>
MC>> MC>>> Have you had a look a the example shiped with castor source archiv. The
MC>>> important files for you are in directories 'src/examples/jdo' and MC>>> 'src/examples/myapp'. See Product and Categorie for a many-many
MC>>> relationship example.
MC>> MC>> MC>> Thx a lot for pointing me at this example, it was of great help for me. MC>> Now I think everything works fine :)!
MC>
MC>Sorry, I still seem to have a problem :(. I can succesfully receive a MC>list of all employees belonging to a particular Department, but the MC>reverse failes. So finding out to which departments an Employee belongs MC>causes problems, it even seems to modify the tables :(.
MC>
MC>I have a solution, but I don't know if it is a good one: I would make to MC>tables: next to category_prod, I would also make a table prod_category MC>(+add this to the mapping), which would (I think) solve my problem. It's MC>only, this doesn't seem very elegant to me :(.


Mattias,

This is not the solution to your problem. You've already got a bridge
table that maps Employee objects to Department objects. I'm willing to
bet that your problem resides with the your objects using bi-directional
relationships (http://www.castor.org/jdo-faq.html#bi-directional). You've
already established that you have a reference from Department to
Employee. But do you have a reference from Employee to Department?

Well, I tried to add it, but then the code alters the data in the database (mysql) :(. I'll try to explain it a little bit more. My "Employee" is a User who can subscribe to a Channel (~Department). The channel needs to know who subscribed to him and a User wants to know which Channels he subscribed too. When I only provided a mapping from Channel to User, I could ask a Channel to give its users. Now that I changed the mapping, my code even changes my data :(.


Let me be more clear:
-----------------mapping-----------------
<mapping>
    <class name="Channel"
           identity="id" key-generator="MAX">
      <description>Channel definition</description>
      <map-to table="channels" xml="channel" />
      <field name="id" type="integer">
        <sql name="id" type="integer" />
        <xml name="id" node="attribute" />
      </field>
      <field name="name" type="string">
        <sql name="name" type="varchar" />
        <xml name="name" node="element" />
      </field>
      <!-- Any number of users can be subscribed to the same
        Channel, a user can belong to any number of Channels-->
      <field name="users" type="User" required="true"
           collection="vector">
        <sql name="user_id"
           many-table="channel_user" many-key="channel_id" />
        <bind-xml name="user" node="element" />
      </field>
    </class>

<!-- Mapping for User -->
<class name="User"
identity="id" key-generator="MAX">
<description>User definition</description>
<map-to table="users" xml="user" />
<field name="id" type="integer">
<sql name="id" type="integer" />
<xml name="id" node="attribute" />
</field>
<field name="jid" type="string" get-method="getJIDAsString" set-method="setJID">
<sql name="jid" type="varchar" />
<xml name="jid" node="element" />
</field>
<!-- Any number of users can be subscribed to the same
Channel, a user can belong to any number of Channels-->
<field name="channels" type="Channel" required="true"
collection="vector">
<sql name="channel_id"
many-table="channel_user" many-key="user_id" />
<bind-xml name="channel" node="element" />
</field>
</class>


</mapping>

-------------------end of mapping-------------------------

-------------------source code ---------------------------
db.begin();
Channel chan = null;
OQLQuery query;
query = db.getOQLQuery("SELECT c from Channel c WHERE name=\"astrid\"");
QueryResults results;
results = query.execute();
while (results.hasMore()) {
  chan = (Channel) results.next();
  System.out.println("---" + chan.getName() + "---");
}
db.commit();
                                        
Vector v = chan.getUsers();
Iterator iter = v.iterator();
while (iter.hasNext())
  System.out.println("**$** " + ((User) iter.next()).getJIDAsString());
------------------end of source code-----------------------

----------------------example data before ------------------
mysql> select * from channel_user;
+---------+------------+
| user_id | channel_id |
+---------+------------+
|       2 |          3 |
|       2 |          1 |
|       1 |          2 |
+---------+------------+

mysql> select * from users;
+------+--------------------+
| id   | jid                |
+------+--------------------+
|    1 | [EMAIL PROTECTED]  |
|    2 | [EMAIL PROTECTED] |
+------+--------------------+

mysql> select id, name, location from channels;
+----+--------+--------------------------------------------------+
| id | name   | location                                         |
+----+--------+--------------------------------------------------+
|  2 | jabber | http://www.jabber.org/news/rss.xml               |
|  1 | astrid | http://student.rug.ac.be/astrid/Share/astrid.rdf |
|  3 | gdvm   | http://users.pandora.be                          |
+----+--------+--------------------------------------------------+
--------------------end of example data before --------------------

--------------------example data after --------------------------
(only the table channels changed the name attribute :(, the other
tables remained as they were)
+----+--------+--------------------------------------------------+
| id | name   | location                                         |
+----+--------+--------------------------------------------------+
|  2 | jabber | http://www.jabber.org/news/rss.xml               |
|  1 | gdvm   | http://student.rug.ac.be/astrid/Share/astrid.rdf |
|  3 | gdvm   | http://users.pandora.be                          |
+----+--------+--------------------------------------------------+
--------------------end of example data after---------------------

 -------------------Output program----------------------------
---gdvm---   (--> should be ---astrid---)
**$** [EMAIL PROTECTED] (--> correct
---------------------End of Output program--------------------


I hope this explains my problem a little bit more and I even more hope that somebody could help me solve this problem ;)


regards
Mattias

----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev




Reply via email to