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 Melonie Brown <me...@mindspring.com> on 2003/04/09 04:15:36 UTC

Query finally works (sort of)

Thanks to everyone that gave me suggestions as to bringing my two tables
together.  Almost everything has fallen into place, except for one small
thing . . . . 

In the code and repository shown below, the join between the two classes
seems to require that the foreign-key field ref ("id") in menuitems join
with the primary key ("menuitemid") in user_privileges.  The only
problem?  "menuitemid" shouldn't be the primary key in user_privileges -
the "userid" should be the primary key.  If I set the "userid" to be the
primary key, then OJB tries to do a join on "userid" to the menuitems
"id" - which won't return any records at all.  

Is it possible to explicitly set the join field?  If so, how?
----------------------------------------
public void testGetAppUserPrivs()
 {
        Criteria crit = new Criteria();
        crit.addEqualTo("usermenu.userid", userid);
        crit.addOrderBy("menuPosition",true);
        crit.addNotNull("menuPosition");
        Query q = QueryFactory.newQuery(MenuItem.class, crit, true);
        Collection results = broker.getCollectionByQuery(q);
        assertTrue(results != null);
        Iterator it = results.iterator();
        while (it.hasNext())
        {
            MenuItem x = (MenuItem) it.next();
            UserPrivileges y = (UserPrivileges) (x.getUsermenu());
            System.out.print("\t" + y.getPrivilege());
            System.out.print("\t" + y.getMenuitemid());
            System.out.print("\t" + x.getLink());
            System.out.println("\t" + y.getUserid());
        }
   }

<class-descriptor
class="mil.army.forscom.menus.businessobjects.UserPrivileges"
     table="user_privileges"   >
      <field-descriptor
      	 name="userid"
         column="userid"
         jdbc-type="VARCHAR"
         primarykey="false"
         autoincrement="false"
      />
      <field-descriptor
      	 name="menuitemid"
         column="menuitemid"
         jdbc-type="INTEGER"
         primarykey="true"
         autoincrement="false"
      />
      <field-descriptor
      	 name="privilege"
         column="privilege"
         jdbc-type="VARCHAR"
         primarykey="false"
         autoincrement="false"
      />           
   </class-descriptor>
   
   <class-descriptor
class="mil.army.forscom.menus.businessobjects.MenuItem"
     table="menuitems"   >
      <field-descriptor
      	 name="id"
      	 id="1"
         column="id"
         jdbc-type="INTEGER"
         primarykey="TRUE"
         autoincrement="false"
      />
      <field-descriptor
      	id="2"
      	 name="description"
         column="description"
         jdbc-type="VARCHAR"
         primarykey="false"
         autoincrement="false"
      />
      <field-descriptor
      		id="3"
      	 name="link"
         column="link"
         jdbc-type="VARCHAR"
         primarykey="false"
         autoincrement="false"
      />
      <field-descriptor
      		id="4"
      	 name="menuPosition"
         column="MenuPosition"
         jdbc-type="VARCHAR"
         primarykey="false"
         autoincrement="false"
      />   
      <reference-descriptor name="usermenu" 
 
class-ref="mil.army.forscom.menus.businessobjects.UserPrivileges"
      	auto-retrieve="true"   
      >
      	<foreignkey field-ref="id"></foreignkey>
      </reference-descriptor>
    
   </class-descriptor>

   UserPrivileges:
    private String userid; 
    private int menuitemid; 
    private String privilege; 
[getters/setters]

MenuItem:
    private int id;
    private String description;
    private String link;    
    private String menuPosition;
    UserPrivileges usermenu;
[getters/setters]