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 Bernd Laengerich <B....@actosoft.de> on 2005/10/25 15:11:30 UTC

Inheritance using special attribute ojbConcreteClass issue

Hi,

is there any way to tell OJB to use a different attribute name for a 
specific class instead of ojbConcreteClass?

The problem is, I have a table A with objects of two different classes 
A und B (both using inheritance) instantiated from it.

Torque refuses the "datasql" target with the error:
Attribute "OjbConcreteClass" was already specified for element "ATABLE".
OJB itself is working fine with it.

Extracts from the classes and the repository.xml:

---------- snip ----------
/**
  * @ojb.class table="ATABLE"
  */
public abstract class BaseA {

   /**
    * @ojb.field column="ID"
    * jdbc-type="INTEGER" primarykey="true"
    */
   protected int    id;
   /**
    * @ojb.field column="A_CLASS_NAME"
    * jdbc-type="VARCHAR" length="255" nullable="false"
    */
   protected String ojbConcreteClass;
   /**
    * @ojb.field column="X_REF" jdbc-type="INTEGER"
    */
   protected int    xId;
   /**
    * @ojb.reference class-ref="org.xyz.X"
    * foreignkey="xId"
    */
   protected X            x;
...
}

public class AImpl extends BaseA {
...
}
---------- snip ----------
/**
  * @ojb.class table="ATABLE"
  */
public class BaseB {

   /**
    * @ojb.field column="ID"
    * jdbc-type="INTEGER" primarykey="true"
    */
   private Integer             id;
   /**
    * @ojb.field column="B_CLASS_NAME"
    * jdbc-type="VARCHAR" length="255" nullable="false"
    */
   protected String ojbConcreteClass;
   /**
    * @ojb.field column="ANY_OTHER"
    * jdbc-type="VARCHAR" length="50" nullable="false"
    */
   protected String justGarbage;
...
}

public class BImpl extends BaseB {
...
}

---------- snip ----------

<class-descriptor
     class="org.xyz.BaseA"
     table="ATABLE"
 >
     <extent-class class-ref="org.xyz.AImpl"/>
     <field-descriptor
         name="id"
         column="ID"
         jdbc-type="INTEGER"
         primarykey="true"
     >
     </field-descriptor>
     <field-descriptor
         name="ojbConcreteClass"
         column="A_CLASS_NAME"
         jdbc-type="VARCHAR"
         nullable="false"
         length="255"
     >
     </field-descriptor>
     <field-descriptor
         name="xId"
         column="X_REF"
         jdbc-type="INTEGER"
     >
     </field-descriptor>
     <reference-descriptor
         name="x"
         class-ref="org.xyz.X"
     >
         <foreignkey field-ref="xId"/>
     </reference-descriptor>
</class-descriptor>
<class-descriptor
     class="org.xyz.AImpl"
     table="ATABLE"
 >
     <field-descriptor
         name="id"
         column="ID"
         jdbc-type="INTEGER"
         primarykey="true"
     >
     </field-descriptor>
     <field-descriptor
         name="ojbConcreteClass"
         column="A_CLASS_NAME"
         jdbc-type="VARCHAR"
         nullable="false"
         length="255"
     >
     </field-descriptor>
     <field-descriptor
         name="xId"
         column="X_REF"
         jdbc-type="INTEGER"
     >
     </field-descriptor>
     <reference-descriptor
         name="x"
         class-ref="org.xyz.X"
     >
         <foreignkey field-ref="xId"/>
     </reference-descriptor>
</class-descriptor>
<class-descriptor
     class="org.xyz.BaseB"
     table="ATABLE"
 >
     <extent-class class-ref="org.xyz.BImpl"/>
     <field-descriptor
         name="id"
         column="ID"
         jdbc-type="INTEGER"
         primarykey="true"
     >
     </field-descriptor>
     <field-descriptor
         name="ojbConcreteClass"
         column="B_CLASS_NAME"
         jdbc-type="VARCHAR"
         nullable="false"
         length="255"
     >
     </field-descriptor>
     <field-descriptor
         name="justGarbage"
         column="ANY_OTHER"
         jdbc-type="VARCHAR"
         nullable="false"
         length="50"
     >
     </field-descriptor>
</class-descriptor>
<class-descriptor
     class="org.xyz.BImpl"
     table="ATABLE"
 >
     <field-descriptor
         name="id"
         column="ID"
         jdbc-type="INTEGER"
         primarykey="true"
     >
     </field-descriptor>
     <field-descriptor
         name="ojbConcreteClass"
         column="B_CLASS_NAME"
         jdbc-type="VARCHAR"
         nullable="false"
         length="255"
     >
     </field-descriptor>
     <field-descriptor
         name="justGarbage"
         column="ANY_OTHER"
         jdbc-type="VARCHAR"
         nullable="false"
         length="50"
     >
     </field-descriptor>
</class-descriptor>

Bernd

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


Re: Inheritance using special attribute ojbConcreteClass issue

Posted by Thomas Dudziak <to...@gmail.com>.
On 10/26/05, Bernd Laengerich <B....@actosoft.de> wrote:

> > As for the error message, for now you can turn off the XDoclet check
> > using the checks attribute as described here:#
>
> It's not XDoclet complaining, but Torque, so is this a Torque issue?
> The error message and stack trace (with -debug) is:

Might be either the XDoclet module or Torque. Could you post the
generated repository file ?

Tom

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


Re: Inheritance using special attribute ojbConcreteClass issue

Posted by Bernd Laengerich <B....@actosoft.de>.
Thomas Dudziak wrote:

> Oh, yes, I missed the BaseB part.

:-) I stripped down from company sources and uglified the names.

> So you're mapping unrelated inheritance hierarchies onto the same
> table ? 

Yes. This is kind of historically based, I designed for different 
classes and tables, but there were 1 to {0,1} relations and our 
database "guru" from the other department "optimized" them into one 
table :-(
As the classes can be used independently, I tried out this way and it 
worked.

 > I'm not sure whether that works in all instances (think:
> queries for the base classes, collections that refer to one of the
> base classes), but if you say that OJB works without problems, then
> all the better.

Seems to work in OJB without problems for my purposes.

> As for the error message, for now you can turn off the XDoclet check
> using the checks attribute as described here:#

It's not XDoclet complaining, but Torque, so is this a Torque issue?
The error message and stack trace (with -debug) is:

[...]
Setting project property: torquetag -> datasql
[...]

datasql:
         [echo] +-----------------------------------------------+
         [echo] | Generating SQL from data XML !                |
         [echo] +-----------------------------------------------+
         [echo]
         [echo] 
--------------------------------------------------------
         [echo]       contextProperties   ="build-torque.properties"
         [echo]       controlTemplate     ="sql/load/Control.vm"
         [echo]       dataDTD 
="ojb_build/build/resources//accept-data.dtd"
         [echo]       dataXmlFile 
="ojb_build/build/resources//accept-data.xml"
         [echo]       outputDirectory     ="ojb_build/build/resources/"
         [echo]       outputFile          ="accept-data.sql"
         [echo]       sqldbmap 
="ojb_build/build/resources//sqldb.map"
         [echo]       targetDatabase      ="mysql"
         [echo]       templatePath        ="ojb_build/templates/"
         [echo]       useClasspath        ="false"
         [echo]       xmlFile 
="ojb_build/build/resources//accept-schema.xml"
         [echo] 
--------------------------------------------------------
         [echo]

[...]

Class org.apache.torque.engine.database.transform.XmlToData$DataRow 
loaded from ant loader (parentFirst)
          [ant] Exiting 
C:\Entwicklung\accept\ojb_build\schema\build-torque.xml.

BUILD FAILED
C:\Entwicklung\accept\build_ojb.xml:375: The following error occurred 
while executing this line:
C:\Entwicklung\accept\ojb_build\schema\build-torque.xml:505: 
org.xml.sax.SAXParseException: Attribute "OjbConcreteClass" was 
already specified for element "AccTerminal".
	at 
org.apache.tools.ant.ProjectHelper.addLocationToBuildException(ProjectHelper.java:539)
	at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:384)
	at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
	at org.apache.tools.ant.Task.perform(Task.java:364)
	at org.apache.tools.ant.Target.execute(Target.java:341)
	at org.apache.tools.ant.Target.performTasks(Target.java:369)
	at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
	at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
	at 
org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
	at 
org.eclipse.ant.internal.core.ant.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
	at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
	at 
org.eclipse.ant.internal.core.ant.InternalAntRunner.run(InternalAntRunner.java:704)
	at 
org.eclipse.ant.internal.core.ant.InternalAntRunner.run(InternalAntRunner.java:455)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.ant.core.AntRunner.run(AntRunner.java:356)
	at 
org.eclipse.ant.internal.ui.launchConfigurations.AntLaunchDelegate$1.run(AntLaunchDelegate.java:215)
	at java.lang.Thread.run(Unknown Source)
Caused by: 
C:\Entwicklung\accept\ojb_build\schema\build-torque.xml:505: 
org.xml.sax.SAXParseException: Attribute "OjbConcreteClass" was 
already specified for element "AccTerminal".
	at 
org.apache.torque.task.TorqueDataSQLTask.initControlContext(TorqueDataSQLTask.java:201)
	at org.apache.velocity.texen.ant.TexenTask.execute(TexenTask.java:480)
	at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
	at org.apache.tools.ant.Task.perform(Task.java:364)
	at org.apache.tools.ant.Target.execute(Target.java:341)
	at org.apache.tools.ant.Target.performTasks(Target.java:369)
	at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
	at 
org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:37)
	at 
org.eclipse.ant.internal.core.ant.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
	at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
	at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:382)
	... 18 more
Caused by: org.xml.sax.SAXParseException: Attribute "OjbConcreteClass" 
was already specified for element "AccTerminal".
	at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
	at javax.xml.parsers.SAXParser.parse(Unknown Source)
	at 
org.apache.torque.engine.database.transform.XmlToData.parseFile(XmlToData.java:99)
	at 
org.apache.torque.task.TorqueDataSQLTask.initControlContext(TorqueDataSQLTask.java:150)
	... 28 more
--- Nested Exception ---
C:\Entwicklung\accept\ojb_build\schema\build-torque.xml:505: 
org.xml.sax.SAXParseException: Attribute "OjbConcreteClass" was 
already specified for element "AccTerminal".
	at 
org.apache.torque.task.TorqueDataSQLTask.initControlContext(TorqueDataSQLTask.java:201)
	at org.apache.velocity.texen.ant.TexenTask.execute(TexenTask.java:480)
	at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
	at org.apache.tools.ant.Task.perform(Task.java:364)
	at org.apache.tools.ant.Target.execute(Target.java:341)
	at org.apache.tools.ant.Target.performTasks(Target.java:369)
	at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
	at 
org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:37)
	at 
org.eclipse.ant.internal.core.ant.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
	at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
	at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:382)
	at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
	at org.apache.tools.ant.Task.perform(Task.java:364)
	at org.apache.tools.ant.Target.execute(Target.java:341)
	at org.apache.tools.ant.Target.performTasks(Target.java:369)
	at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
	at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
	at 
org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
	at 
org.eclipse.ant.internal.core.ant.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
	at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
	at 
org.eclipse.ant.internal.core.ant.InternalAntRunner.run(InternalAntRunner.java:704)
	at 
org.eclipse.ant.internal.core.ant.InternalAntRunner.run(InternalAntRunner.java:455)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.ant.core.AntRunner.run(AntRunner.java:356)
	at 
org.eclipse.ant.internal.ui.launchConfigurations.AntLaunchDelegate$1.run(AntLaunchDelegate.java:215)
	at java.lang.Thread.run(Unknown Source)
Caused by: org.xml.sax.SAXParseException: Attribute "OjbConcreteClass" 
was already specified for element "AccTerminal".
	at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
	at javax.xml.parsers.SAXParser.parse(Unknown Source)
	at 
org.apache.torque.engine.database.transform.XmlToData.parseFile(XmlToData.java:99)
	at 
org.apache.torque.task.TorqueDataSQLTask.initControlContext(TorqueDataSQLTask.java:150)
	... 28 more
--- Nested Exception ---
org.xml.sax.SAXParseException: Attribute "OjbConcreteClass" was 
already specified for element "AccTerminal".
	at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
	at javax.xml.parsers.SAXParser.parse(Unknown Source)
	at 
org.apache.torque.engine.database.transform.XmlToData.parseFile(XmlToData.java:99)
	at 
org.apache.torque.task.TorqueDataSQLTask.initControlContext(TorqueDataSQLTask.java:150)
	at org.apache.velocity.texen.ant.TexenTask.execute(TexenTask.java:480)
	at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
	at org.apache.tools.ant.Task.perform(Task.java:364)
	at org.apache.tools.ant.Target.execute(Target.java:341)
	at org.apache.tools.ant.Target.performTasks(Target.java:369)
	at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
	at 
org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:37)
	at 
org.eclipse.ant.internal.core.ant.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30)
	at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
	at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:382)
	at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
	at org.apache.tools.ant.Task.perform(Task.java:364)
	at org.apache.tools.ant.Target.execute(Target.java:341)
	at org.apache.tools.ant.Target.performTasks(Target.java:369)
	at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
	at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
	at 
org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
	at 
org.eclipse.ant.internal.core.ant.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
	at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
	at 
org.eclipse.ant.internal.core.ant.InternalAntRunner.run(InternalAntRunner.java:704)
	at 
org.eclipse.ant.internal.core.ant.InternalAntRunner.run(InternalAntRunner.java:455)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.ant.core.AntRunner.run(AntRunner.java:356)
	at 
org.eclipse.ant.internal.ui.launchConfigurations.AntLaunchDelegate$1.run(AntLaunchDelegate.java:215)
	at java.lang.Thread.run(Unknown Source)

Total time: 12 seconds


Bernd

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


Re: Inheritance using special attribute ojbConcreteClass issue

Posted by Thomas Dudziak <to...@gmail.com>.
On 10/26/05, Bernd Laengerich <B....@actosoft.de> wrote:
> Thomas Dudziak wrote:
>
> > I think the error comes from the XDoclet module, not from Torque.
> > The problem is that you should not redefine the field in the subclass
>
> I haven't redefined it in the java code, so you think it should not
> appear in the repository xml for the subclass?

Oh, yes, I missed the BaseB part.
So you're mapping unrelated inheritance hierarchies onto the same
table ? I'm not sure whether that works in all instances (think:
queries for the base classes, collections that refer to one of the
base classes), but if you say that OJB works without problems, then
all the better.
As for the error message, for now you can turn off the XDoclet check
using the checks attribute as described here:

http://db.apache.org/ojb/docu/guides/xdoclet-module.html#Usage

Please be so kind and add an issue for this in OJB's JIRA, attaching
the sample source code and error message/stack trace.

Tom

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


Re: Inheritance using special attribute ojbConcreteClass issue

Posted by Bernd Laengerich <B....@actosoft.de>.
Thomas Dudziak wrote:

> I think the error comes from the XDoclet module, not from Torque.
> The problem is that you should not redefine the field in the subclass

I haven't redefined it in the java code, so you think it should not 
appear in the repository xml for the subclass?

> The usual pattern is to define the field in the base class, and
> initialize it either directly on in the constructor, like so:
> 
> private String ojbConcreteClass = getClass();

OK, initialization is done in a similar way in the constructor. 
Everything works fine with OJB and our code. As we wanted to migrate 
to a different data base vendor, I tried Torque to generate the 
required sql. It already worked some time ago, but changes to the 
database required different classes mapped to one table then.

I will try to patch the generated repository xml manually...

Bernd

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


Re: Inheritance using special attribute ojbConcreteClass issue

Posted by Thomas Dudziak <to...@gmail.com>.
On 10/25/05, Bernd Laengerich <B....@actosoft.de> wrote:
> Hi,
>
> is there any way to tell OJB to use a different attribute name for a
> specific class instead of ojbConcreteClass?
>
> The problem is, I have a table A with objects of two different classes
> A und B (both using inheritance) instantiated from it.
>
> Torque refuses the "datasql" target with the error:
> Attribute "OjbConcreteClass" was already specified for element "ATABLE".
> OJB itself is working fine with it.

I think the error comes from the XDoclet module, not from Torque.
The problem is that you should not redefine the field in the subclass
(you don't have to, anyway, as its protected and thus already visible
in the subclass), because when using reflection there is no guarantee
which one (of the base class or the sub class) is used.
The usual pattern is to define the field in the base class, and
initialize it either directly on in the constructor, like so:

private String ojbConcreteClass = getClass();

Btw, this way you don't even have to make it protected as the
subclasses don't do much with it anyway.

Tom

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