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 Maxim Kuleshov <ku...@org.vrn.ru> on 2009/01/29 13:18:14 UTC

extents, mapping classes to joined tables and collection-type field

Hi!

How could I configure OJB to process such scheme:

I have base class A and at least one its subclass B. Each class uses
different DB tables, joined to each other. In following examples I have
TABLE_B references to TABLE_A by foreign key PARENT_ID.

There is also third class, lets say 'ObjList' having 1:n relation to
base class A.

Java code looks like this:

package objs;

/* @ojb.class table="TABLE_A"
 * @ojb.extent class-ref="objs.B"
 * @ojb.field name="listId"
 *            column="list_id"
 *            jdbc-type="INTEGER"
 */
class A {
  /* @ojb.field column="id"
   *            jdbc-type="INTEGER"
   *            primarykey="true"
   */
  int id;

  /* @ojb.field column="field1"
   *            jdbc-type="INTEGER" 
   */
  int field1;

  /* @ojb.field column="field2"
   *            jdbc-type="INTEGER"
   */
  int field2;

  /* @ojb.reference foreignkey="listId"
   *                auto-retrieve="true"
   *                auto-update="link"
   *                auto-delete="link"
   */
  ObjsList list;
  
}

/* @ojb.class table="TABLE_B"
 *            include-inherited="false"
 * @ojb.field name="id"
 *            jdbc-type="INTEGER"
 *            column="id"
 *            primarykey="true"
 * @ojb.field name="parentId"
 *            column="parent_id"
 *            jdbc-type="INTEGER"
 *            access="anonymous"
 * @ojb.reference class-ref="objs.A"
 *                auto-retrieve="true"
 *                auto-update="object"
 *                auto-delete="object"
 *                foreignkey="parentId"
 */
class B extends A {
  /* @ojb.field column="field3"
   *            jdbc-type="INTEGER" */
  int field3;
}

class ObjsList {
  ... various fields ...

/* @ojb.collection
  * element-class-ref="objs.A"
  * foreignkey="listId"
  * auto-retreive="true"
  * auto-update="link"
  * audo-delete="none"
  * proxy="true"
  */
  List<A> objs;
}

That's scheme gives me error during XDoclet stage:

"The collection entries in class ojbs.ObjsList specifies a foreignkey
listId that is not a persistent field in the element class (or its
subclass) objs.B"

If I include declaration of listId field to class "B" (via
include-inherited="true" or place separate ojb.field to class B, then
OJB suppose TABLE_B have LIST_ID column, though it doesn't.

The following SQL generated when method getObjs() invoked on ObjsList
instance.

SELECT count(*) FROM TABLE_B A0 INNER JOIN
TABLE_A A1 ON A0.parent_id=A1.id WHERE A0.list_id = ?

Why OJB forces class B have listId?

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


Re: extents, mapping classes to joined tables and collection-type field

Posted by Maxim Kuleshov <ku...@org.vrn.ru>.
On Thu, 29 Jan 2009 15:18:14 +0300
Maxim Kuleshov <ku...@org.vrn.ru> wrote:

> That's scheme gives me error during XDoclet stage:
> 
> "The collection entries in class ojbs.ObjsList specifies a foreignkey
> listId that is not a persistent field in the element class (or its
> subclass) objs.B"

Well, this problem has been solved. The answer is I mixed up extents and
multiple-joined tables concepts.

But another problem still persists. Then storing instance of classB
its foreign key to classA (prentId), declared as anonymous is not
set automatically, so reference between table_a and table_b is lost and
when loading classB all fields declared in classA are nulls.

The only way to preserve references is to use primary key of classB
(id) as a foreign key to classA. But how to force sequence-manager
generate its value only once (if every class has its own instance of
id).

I use XDoclet, and it doesn't allow to inherit particullar field.
Moreover, it doesn't allow to ignore duplicate fields, as listId among
then and class ObjsList has collection back-referencing via that field.

If I put obj.field declaration with name attribute it always become
anonymous (which is not true).



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