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 Dennis Portello <dp...@speedfactory.net> on 2003/11/07 10:43:54 UTC

Problem storing collections with ODMG

Hello..

I've been pouring over google and the ojb site for hours and can not find the 
info I need to solve this problem before me.

Basically I have 2 entity tables (user and role) with and indirection table 
(user_role). I'm using the ODMG interface. My User bean contains a property 
called "Roles" of the type List. 

I have no problem storing single objects, but when I attempt to store a User 
instance populated with a list of roles, nothing appears in user_role.

Does the ODMG interface not support this storing of object graphs?

////// Here is the DDL //////
CREATE TABLE user (
  id BIGINT NOT NULL AUTO_INCREMENT,
  name VARCHAR (20) NOT NULL,
  credential VARCHAR (32) NOT NULL,
    PRIMARY KEY(id),
    UNIQUE (name)
) Type=InnoDB;

CREATE TABLE role (
  id BIGINT NOT NULL AUTO_INCREMENT,
  code VARCHAR (20) NOT NULL,
  description VARCHAR (40) NOT NULL,
    PRIMARY KEY(id),
    UNIQUE (code)
) Type=InnoDB;

CREATE TABLE user_role (
  id BIGINT NOT NULL AUTO_INCREMENT,
  user_id BIGINT NOT NULL,
  role_id BIGINT NOT NULL,
    PRIMARY KEY(id),
    FOREIGN KEY (user_id) REFERENCES user (id),
    FOREIGN KEY (role_id) REFERENCES role (id),
    INDEX user_role_1_idx (user_id),
    INDEX user_role_2_idx (role_id)
) Type=InnoDB;

///// repository_user.xml /////
<class-descriptor
  class="com.xyz.businessobjects.UserBO"
  table="user">
  <field-descriptor
    name="id"
    column="id"
    jdbc-type="BIGINT"
    primarykey="true"
    autoincrement="true"/>
  <field-descriptor
    name="name"
    column="name"
    nullable="false"
    jdbc-type="VARCHAR"/>
  <field-descriptor
    name="credential"
    column="credential"
    nullable="false"
    jdbc-type="VARCHAR"/>
  <collection-descriptor
    name="roles"
    element-class-ref="com.xyz.businessobjects.RoleBO"
    indirection-table="user_role"
    auto-retrieve="true"
    auto-update="true">
    <fk-pointing-to-this-class
      column="user_id"/>
    <fk-pointing-to-element-class
      column="role_id"/>
  </collection-descriptor>
</class-descriptor>

<class-descriptor
  class="com.xyz.businessobjects.RoleBO"
  table="role">
  <field-descriptor
    name="id"
    column="id"
    jdbc-type="BIGINT"
    primarykey="true"
    autoincrement="true"/>
  <field-descriptor
    name="code"
    column="code"
    nullable="false"
    jdbc-type="VARCHAR"/>
  <field-descriptor
    name="description"
    column="description"
    nullable="false"
    jdbc-type="VARCHAR"/>
</class-descriptor>

//// methods using ODMG ////
public void setUser(UserBO user)
 throws ServiceException {

  Transaction tx = null;

  try {
      tx = odmg.newTransaction();
      tx.begin();

      tx.lock(user, Transaction.WRITE);

      tx.commit();
  } catch( LockNotGrantedException lnge ) {
    throw new ServiceException(lnge);
  }
};

public UserBO getUser(String name)
 throws ServiceException {

  UserBO user = null;

  try{
    System.out.println(UserBO.class.getName());

    String queryStr = "select userBO from "
     + UserBO.class.getName();

    String whereClause = " where name = $1";
    OQLQuery query = this.createQuery( queryStr
     + whereClause );

    query.bind( name );

    List users = (List) query.execute();

    user = (UserBO)users.get(0);

  }catch( QueryException ex ){
    ex.printStackTrace();
    throw new ServiceException( ex );
  }
  return user;
};

public void setRole(RoleBO role)
 throws ServiceException {

  Transaction tx = null;

  try {
      tx = odmg.newTransaction();
      tx.begin();

      tx.lock(role, Transaction.WRITE);

      tx.commit();
  } catch( LockNotGrantedException lnge ) {
    throw new ServiceException(lnge);
  }

};


public List getRoles() throws ServiceException {

  List roles = null;

  try{
    String queryStr = "select role from "
     + RoleBO.class.getName();

    OQLQuery query = this.createQuery( queryStr );

    roles = (List) query.execute();

  }catch( QueryException ex ){
    ex.printStackTrace();
    throw new ServiceException( ex );
  }

  return roles;
};

Thanks,
Dennis


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


Re: Problem storing collections with ODMG

Posted by Dennis Portello <dp...@speedfactory.net>.
I'm using db-ojb-1.0.rc4 btw

thanks,
Dennis

On Friday 07 November 2003 04:43, Dennis Portello wrote:
> Hello..
>
> I've been pouring over google and the ojb site for hours and can not find
> the info I need to solve this problem before me.
>
> Basically I have 2 entity tables (user and role) with and indirection table
> (user_role). I'm using the ODMG interface. My User bean contains a property
> called "Roles" of the type List.
>
> I have no problem storing single objects, but when I attempt to store a
> User instance populated with a list of roles, nothing appears in user_role.
>
> Does the ODMG interface not support this storing of object graphs?
>
> ////// Here is the DDL //////
> CREATE TABLE user (
>   id BIGINT NOT NULL AUTO_INCREMENT,
>   name VARCHAR (20) NOT NULL,
>   credential VARCHAR (32) NOT NULL,
>     PRIMARY KEY(id),
>     UNIQUE (name)
> ) Type=InnoDB;
>
> CREATE TABLE role (
>   id BIGINT NOT NULL AUTO_INCREMENT,
>   code VARCHAR (20) NOT NULL,
>   description VARCHAR (40) NOT NULL,
>     PRIMARY KEY(id),
>     UNIQUE (code)
> ) Type=InnoDB;
>
> CREATE TABLE user_role (
>   id BIGINT NOT NULL AUTO_INCREMENT,
>   user_id BIGINT NOT NULL,
>   role_id BIGINT NOT NULL,
>     PRIMARY KEY(id),
>     FOREIGN KEY (user_id) REFERENCES user (id),
>     FOREIGN KEY (role_id) REFERENCES role (id),
>     INDEX user_role_1_idx (user_id),
>     INDEX user_role_2_idx (role_id)
> ) Type=InnoDB;
>
> ///// repository_user.xml /////
> <class-descriptor
>   class="com.xyz.businessobjects.UserBO"
>   table="user">
>   <field-descriptor
>     name="id"
>     column="id"
>     jdbc-type="BIGINT"
>     primarykey="true"
>     autoincrement="true"/>
>   <field-descriptor
>     name="name"
>     column="name"
>     nullable="false"
>     jdbc-type="VARCHAR"/>
>   <field-descriptor
>     name="credential"
>     column="credential"
>     nullable="false"
>     jdbc-type="VARCHAR"/>
>   <collection-descriptor
>     name="roles"
>     element-class-ref="com.xyz.businessobjects.RoleBO"
>     indirection-table="user_role"
>     auto-retrieve="true"
>     auto-update="true">
>     <fk-pointing-to-this-class
>       column="user_id"/>
>     <fk-pointing-to-element-class
>       column="role_id"/>
>   </collection-descriptor>
> </class-descriptor>
>
> <class-descriptor
>   class="com.xyz.businessobjects.RoleBO"
>   table="role">
>   <field-descriptor
>     name="id"
>     column="id"
>     jdbc-type="BIGINT"
>     primarykey="true"
>     autoincrement="true"/>
>   <field-descriptor
>     name="code"
>     column="code"
>     nullable="false"
>     jdbc-type="VARCHAR"/>
>   <field-descriptor
>     name="description"
>     column="description"
>     nullable="false"
>     jdbc-type="VARCHAR"/>
> </class-descriptor>
>
> //// methods using ODMG ////
> public void setUser(UserBO user)
>  throws ServiceException {
>
>   Transaction tx = null;
>
>   try {
>       tx = odmg.newTransaction();
>       tx.begin();
>
>       tx.lock(user, Transaction.WRITE);
>
>       tx.commit();
>   } catch( LockNotGrantedException lnge ) {
>     throw new ServiceException(lnge);
>   }
> };
>
> public UserBO getUser(String name)
>  throws ServiceException {
>
>   UserBO user = null;
>
>   try{
>     System.out.println(UserBO.class.getName());
>
>     String queryStr = "select userBO from "
>      + UserBO.class.getName();
>
>     String whereClause = " where name = $1";
>     OQLQuery query = this.createQuery( queryStr
>      + whereClause );
>
>     query.bind( name );
>
>     List users = (List) query.execute();
>
>     user = (UserBO)users.get(0);
>
>   }catch( QueryException ex ){
>     ex.printStackTrace();
>     throw new ServiceException( ex );
>   }
>   return user;
> };
>
> public void setRole(RoleBO role)
>  throws ServiceException {
>
>   Transaction tx = null;
>
>   try {
>       tx = odmg.newTransaction();
>       tx.begin();
>
>       tx.lock(role, Transaction.WRITE);
>
>       tx.commit();
>   } catch( LockNotGrantedException lnge ) {
>     throw new ServiceException(lnge);
>   }
>
> };
>
>
> public List getRoles() throws ServiceException {
>
>   List roles = null;
>
>   try{
>     String queryStr = "select role from "
>      + RoleBO.class.getName();
>
>     OQLQuery query = this.createQuery( queryStr );
>
>     roles = (List) query.execute();
>
>   }catch( QueryException ex ){
>     ex.printStackTrace();
>     throw new ServiceException( ex );
>   }
>
>   return roles;
> };
>
> Thanks,
> Dennis
>
>
> ---------------------------------------------------------------------
> 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