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 Clayton Webb <cw...@oscardog.org> on 2004/03/02 20:13:45 UTC

inserting "new" roles when retrieving user

For some reason, whenever I retrieve a user or users, the roles 
associated with that user get inserted, again, into the database. My 
repository_user.xml is included. I am using an anonymous key for the 
Role object. I originally thought this was happening because I had 
overridden equals and hashCode but I removed that and it still does it. 
What could I be doing wrong? Thanks in advance.

--clayton

   <class-descriptor
         class="org.oscardog.miniapp.model.User"
         table="USERS"
   >
      <field-descriptor
         name="userName"
         column="USER_NAME"
         jdbc-type="VARCHAR"
         primarykey="true"
      />
      <field-descriptor
         name="lastName"
         column="LAST_NAME"
         jdbc-type="VARCHAR"
      />
      <field-descriptor
         name="firstName"
         column="FIRST_NAME"
         jdbc-type="VARCHAR"
      />
      <field-descriptor
         name="password"
         column="PASSWORD"
         jdbc-type="VARCHAR"
      />
      <collection-descriptor
         name="roles"
         element-class-ref="org.oscardog.miniapp.model.Role"
         auto-retrieve="true"
         auto-update="true"
         indirection-table="USERS_ROLES"
      >
          <fk-pointing-to-this-class column="USER_NAME"/>
          <fk-pointing-to-element-class column="ROLE_ID"/>
      </collection-descriptor>
   </class-descriptor>

   <class-descriptor
         class="org.oscardog.miniapp.model.Role"
         table="ROLES"
   >
      <field-descriptor
         name="id"
         column="ID"
         jdbc-type="NUMERIC"
         primarykey="true"
         autoincrement="true"
         access="anonymous"
      />
      <field-descriptor
         name="role"
         column="ROLE"
         jdbc-type="VARCHAR"
      />
      <collection-descriptor
         name="users"
         element-class-ref="org.oscardog.miniapp.model.User"
         auto-retrieve="true"
         auto-update="true"
         indirection-table="USERS_ROLES"
      >
          <fk-pointing-to-this-class column="ROLE_ID"/>
          <fk-pointing-to-element-class column="USER_NAME"/>
      </collection-descriptor>
   </class-descriptor>


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: inserting "new" roles when retrieving user

Posted by Clayton Webb <cw...@oscardog.org>.
Thought it might be a good idea to include the code I wrote as well.
Here it is:

    snippet from UserBroker.java
    /**
     * Retrieve a user by username.
     *
     * @param userName a <code>String</code> value
     * @return an <code>User</code> value
     * @exception MiniappDBException if an error occurs
     */
    public User retrieveUserByUserName(String userName) throws 
MiniappDBException {
        log.trace("retrieveUserByUserName started");

        StringBuffer oql = new StringBuffer("select users from ");
        oql.append(User.class.getName()).append(" where user_name = $1");
       
        DList results = MiniappBroker.getInstance()
            .execute(oql.toString(), new Object[]{userName});
        User user = (User) results.iterator().next();

        return user;
    }



    snippet from MiniappBroker.java
    private static final MiniappBroker instance = new MiniappBroker();

    private Implementation _implementation = null;

    private MiniappBroker() {
    }

    public static MiniappBroker getInstance() {
        return instance;
    }

    private Implementation getImplementation() throws MiniappDBException {
        log.trace("getImplementation started");
        if (_implementation == null) {
            _implementation = OJB.getInstance();
            Database database = _implementation.newDatabase();

            try {
                database.open("miniappAccess", Database.OPEN_READ_WRITE);
            } catch(ODMGException odmge) {
                throw new MiniappDBException(odmge);
            }
        }

        return _implementation;
    }

    DList execute(String oql, Object[] parameters) throws 
MiniappDBException {
        log.trace("execute started");
        DList results = null;
        Transaction tx = null;
        try {
            tx = getImplementation().newTransaction();
            tx.begin();
            OQLQuery query = getImplementation().newOQLQuery();
            query.create(oql);

            for (int index = 0; index < parameters.length; index++) {
                query.bind(parameters[index]);
            }
            results = (DList) query.execute();
            tx.commit();
        } catch(QueryInvalidException qie) {
            if (tx != null) {
                tx.abort();
            }
            throw new MiniappDBException(qie);
        } catch(QueryParameterCountInvalidException qpcie) {
            if (tx != null) {
                tx.abort();
            }
            throw new MiniappDBException(qpcie);
        } catch(QueryException qe) {
            if (tx != null) {
                tx.abort();
            }
            throw new MiniappDBException(qe);
        } catch(Exception e) {
            if (tx != null) {
                tx.abort();
            }
            throw new MiniappDBException(e);
        }

        return results;
    }


Clayton Webb wrote:

> For some reason, whenever I retrieve a user or users, the roles 
> associated with that user get inserted, again, into the database. My 
> repository_user.xml is included. I am using an anonymous key for the 
> Role object. I originally thought this was happening because I had 
> overridden equals and hashCode but I removed that and it still does 
> it. What could I be doing wrong? Thanks in advance.
>
> --clayton
>
>   <class-descriptor
>         class="org.oscardog.miniapp.model.User"
>         table="USERS"
>   >
>      <field-descriptor
>         name="userName"
>         column="USER_NAME"
>         jdbc-type="VARCHAR"
>         primarykey="true"
>      />
>      <field-descriptor
>         name="lastName"
>         column="LAST_NAME"
>         jdbc-type="VARCHAR"
>      />
>      <field-descriptor
>         name="firstName"
>         column="FIRST_NAME"
>         jdbc-type="VARCHAR"
>      />
>      <field-descriptor
>         name="password"
>         column="PASSWORD"
>         jdbc-type="VARCHAR"
>      />
>      <collection-descriptor
>         name="roles"
>         element-class-ref="org.oscardog.miniapp.model.Role"
>         auto-retrieve="true"
>         auto-update="true"
>         indirection-table="USERS_ROLES"
>      >
>          <fk-pointing-to-this-class column="USER_NAME"/>
>          <fk-pointing-to-element-class column="ROLE_ID"/>
>      </collection-descriptor>
>   </class-descriptor>
>
>   <class-descriptor
>         class="org.oscardog.miniapp.model.Role"
>         table="ROLES"
>   >
>      <field-descriptor
>         name="id"
>         column="ID"
>         jdbc-type="NUMERIC"
>         primarykey="true"
>         autoincrement="true"
>         access="anonymous"
>      />
>      <field-descriptor
>         name="role"
>         column="ROLE"
>         jdbc-type="VARCHAR"
>      />
>      <collection-descriptor
>         name="users"
>         element-class-ref="org.oscardog.miniapp.model.User"
>         auto-retrieve="true"
>         auto-update="true"
>         indirection-table="USERS_ROLES"
>      >
>          <fk-pointing-to-this-class column="ROLE_ID"/>
>          <fk-pointing-to-element-class column="USER_NAME"/>
>      </collection-descriptor>
>   </class-descriptor>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org