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 Tino Schöllhorn <t....@tiscali.de> on 2004/06/14 08:46:37 UTC

Inheritance: One Table

Hi,

I have I inheritance-problem. I want to map two classes to one table. 
They are defined as followed:

class A {
	int id;
	protected String ojbConcreteClass;
	// some attributes	
}

class B extends A {
	// some additional attributes
}


I created class-descriptors for each of the table:

<class-descriptor class="A" table="table">
	// field-descriptors for A inluding ojbConcreteClass
</class>

and


<class-descriptor class="B" table="table" extends="A">
	// additional field-descriptors for B
</class>


So: When I try to store an instance of Type A everything is fine. But 
when I try to store an instance of Type B OJB complains:

Caused by: 
org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException: OJB 
needs at least one primary key attribute for class 
objectsRealClass=class kos.wnetz.om.RelationHistoryEvent, 
objectTopLevelClass=class kos.wnetz.om.RelationHistoryEvent
	at org.apache.ojb.broker.Identity.checkForPrimaryKeys(Identity.java:245)
	at org.apache.ojb.broker.Identity.init(Identity.java:121)


So I also tried to include the field-descriptors of the superclass in 
the class-descriptor for class B. But then I get an even worse error:

Caused by: java.sql.SQLException: Parameter index out of range (8 > 7).
	at 
com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:1940)
	at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:1075)
	at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:766)
	at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:892)
	at 
org.apache.ojb.broker.platforms.PlatformDefaultImpl.setObjectForStatement(PlatformDefaultImpl.java:227)
	at 
org.apache.ojb.broker.platforms.PlatformMySQLImpl.setObjectForStatement(PlatformMySQLImpl.java:87)
	at 
org.apache.ojb.broker.accesslayer.StatementManager.bindInsert(StatementManager.java:449)
	at 
org.apache.ojb.broker.accesslayer.JdbcAccessImpl.executeInsert(JdbcAccessImpl.java:193)
	... 47 more

So obviously I am missing somthing. do you have any suggestions?

Tino


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


Re: Inheritance: One Table

Posted by Thomas Dudziak <to...@first.fhg.de>.
Tino Schöllhorn wrote:

> thanks now it works. But why do I have to map the attributes of the 
> superclass in the subclass again? Is this intended or is the 
> inheritance of descriptors a not yet enhancement?

Yes, this is by intention because you don't necessarily want to have all 
persistent fields of the superclass in the subclass. For the 1.1 we 
might make this a bit more comfortable by adding a flag to the class 
descriptor that defines whether the the features shall be inherited 
(with auto inheritance the default).

Tom


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


Re: Inheritance: One Table

Posted by Tino Schöllhorn <t....@tiscali.de>.
Hi,

thanks now it works. But why do I have to map the attributes of the 
superclass in the subclass again? Is this intended or is the inheritance 
of descriptors a not yet enhancement?

Regards
Tino

Thomas Dudziak wrote:

> Tino Schöllhorn wrote:
> 
>> Hi,
>>
>> I have I inheritance-problem. I want to map two classes to one table. 
>> They are defined as followed:
>>
>> class A {
>>     int id;
>>     protected String ojbConcreteClass;
>>     // some attributes   }
>>
>> class B extends A {
>>     // some additional attributes
>> }
>>
>>
>> I created class-descriptors for each of the table:
>>
>> <class-descriptor class="A" table="table">
>>     // field-descriptors for A inluding ojbConcreteClass
>> </class>
>>
>> and
>>
>>
>> <class-descriptor class="B" table="table" extends="A">
>>     // additional field-descriptors for B
>> </class>
>>
>>
>> So: When I try to store an instance of Type A everything is fine. But 
>> when I try to store an instance of Type B OJB complains:
>>
>> Caused by: 
>> org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException: 
>> OJB needs at least one primary key attribute for class 
>> objectsRealClass=class kos.wnetz.om.RelationHistoryEvent, 
>> objectTopLevelClass=class kos.wnetz.om.RelationHistoryEvent
>>     at 
>> org.apache.ojb.broker.Identity.checkForPrimaryKeys(Identity.java:245)
>>     at org.apache.ojb.broker.Identity.init(Identity.java:121)
>>
>>
>> So I also tried to include the field-descriptors of the superclass in 
>> the class-descriptor for class B. But then I get an even worse error:
>>
>> Caused by: java.sql.SQLException: Parameter index out of range (8 > 7).
> 
> 
> I don't know where this exception comes from, but in order to describe a 
> super-subtype relationship you should use something like:
> 
> <class-descriptor class="A" table="table">
>     <extent-class class-ref="B"/>
>     // field-descriptors for A inluding ojbConcreteClass
> </class>
> 
> and
> 
> <class-descriptor class="B" table="table">
>     // field-descriptors for B including ojbConcreteClass
> </class>
> 
> The extent-class is inverse to the extends/implements in Java (i.e. 
> specified at the supertype), and also note that the class-descriptor for 
> B contains all attributes of A (there is no automatic 'inheritance' of 
> the descriptors in the repository.xml file) including the 
> ojbConcreteClass attribute.
> 
> Tom


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


Re: Inheritance: One Table

Posted by Thomas Dudziak <to...@first.fhg.de>.
Tino Schöllhorn wrote:

> Hi,
>
> unfortunately I still have one problem:
>
> I use a xml-parser to pre-validate the repository.xml-file. This 
> Parser now complains that field-descriptors cannot be used when using 
> extent. I checked that with the DTD and I think it is correct:
>
> <!ELEMENT class-descriptor
>     ((documentation?, extent-class+, attribute*) |
>     (documentation?, object-cache?, extent-class*, field-descriptor+,
>      reference-descriptor*, collection-descriptor*,
>      index-descriptor*, attribute*,
>      insert-procedure?, update-procedure?, delete-procedure?))>
>
> But the thing is: OJB works correctly. So what do I do now? Should I 
> ignore this warning? Or do I have to create a not-persistent abstract
> superclass?

I think, your XML parser is in error here (it probably matches the first 
branch as soon as it sees the extent-class) because extent-class + 
field-descriptor is definitely allowed (second branch). If I'm not 
mistaken, OJB also uses the XML parser with validation turned on, so you 
should not have to validate the repository.xml file, anyway.

Tom


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


Re: Inheritance: One Table

Posted by Tino Schöllhorn <t....@tiscali.de>.
Hi,

unfortunately I still have one problem:

I use a xml-parser to pre-validate the repository.xml-file. This Parser 
now complains that field-descriptors cannot be used when using extent. I 
checked that with the DTD and I think it is correct:

<!ELEMENT class-descriptor
	((documentation?, extent-class+, attribute*) |
	(documentation?, object-cache?, extent-class*, field-descriptor+,
	 reference-descriptor*, collection-descriptor*,
      index-descriptor*, attribute*,
      insert-procedure?, update-procedure?, delete-procedure?))>

But the thing is: OJB works correctly. So what do I do now? Should I 
ignore this warning? Or do I have to create a not-persistent abstract
superclass?

Tino



Thomas Dudziak wrote:

> Tino Schöllhorn wrote:
> 
>> Hi,
>>
>> I have I inheritance-problem. I want to map two classes to one table. 
>> They are defined as followed:
>>
>> class A {
>>     int id;
>>     protected String ojbConcreteClass;
>>     // some attributes   }
>>
>> class B extends A {
>>     // some additional attributes
>> }
>>
>>
>> I created class-descriptors for each of the table:
>>
>> <class-descriptor class="A" table="table">
>>     // field-descriptors for A inluding ojbConcreteClass
>> </class>
>>
>> and
>>
>>
>> <class-descriptor class="B" table="table" extends="A">
>>     // additional field-descriptors for B
>> </class>
>>
>>
>> So: When I try to store an instance of Type A everything is fine. But 
>> when I try to store an instance of Type B OJB complains:
>>
>> Caused by: 
>> org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException: 
>> OJB needs at least one primary key attribute for class 
>> objectsRealClass=class kos.wnetz.om.RelationHistoryEvent, 
>> objectTopLevelClass=class kos.wnetz.om.RelationHistoryEvent
>>     at 
>> org.apache.ojb.broker.Identity.checkForPrimaryKeys(Identity.java:245)
>>     at org.apache.ojb.broker.Identity.init(Identity.java:121)
>>
>>
>> So I also tried to include the field-descriptors of the superclass in 
>> the class-descriptor for class B. But then I get an even worse error:
>>
>> Caused by: java.sql.SQLException: Parameter index out of range (8 > 7).
> 
> 
> I don't know where this exception comes from, but in order to describe a 
> super-subtype relationship you should use something like:
> 
> <class-descriptor class="A" table="table">
>     <extent-class class-ref="B"/>
>     // field-descriptors for A inluding ojbConcreteClass
> </class>
> 
> and
> 
> <class-descriptor class="B" table="table">
>     // field-descriptors for B including ojbConcreteClass
> </class>
> 
> The extent-class is inverse to the extends/implements in Java (i.e. 
> specified at the supertype), and also note that the class-descriptor for 
> B contains all attributes of A (there is no automatic 'inheritance' of 
> the descriptors in the repository.xml file) including the 
> ojbConcreteClass attribute.
> 
> Tom


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


Re: Inheritance: One Table

Posted by Thomas Dudziak <to...@first.fhg.de>.
Tino Schöllhorn wrote:

> Hi,
> 
> I have I inheritance-problem. I want to map two classes to one table. 
> They are defined as followed:
> 
> class A {
>     int id;
>     protected String ojbConcreteClass;
>     // some attributes   
> }
> 
> class B extends A {
>     // some additional attributes
> }
> 
> 
> I created class-descriptors for each of the table:
> 
> <class-descriptor class="A" table="table">
>     // field-descriptors for A inluding ojbConcreteClass
> </class>
> 
> and
> 
> 
> <class-descriptor class="B" table="table" extends="A">
>     // additional field-descriptors for B
> </class>
> 
> 
> So: When I try to store an instance of Type A everything is fine. But 
> when I try to store an instance of Type B OJB complains:
> 
> Caused by: 
> org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException: OJB 
> needs at least one primary key attribute for class 
> objectsRealClass=class kos.wnetz.om.RelationHistoryEvent, 
> objectTopLevelClass=class kos.wnetz.om.RelationHistoryEvent
>     at 
> org.apache.ojb.broker.Identity.checkForPrimaryKeys(Identity.java:245)
>     at org.apache.ojb.broker.Identity.init(Identity.java:121)
> 
> 
> So I also tried to include the field-descriptors of the superclass in 
> the class-descriptor for class B. But then I get an even worse error:
> 
> Caused by: java.sql.SQLException: Parameter index out of range (8 > 7).

I don't know where this exception comes from, but in order to describe a 
super-subtype relationship you should use something like:

<class-descriptor class="A" table="table">
     <extent-class class-ref="B"/>
     // field-descriptors for A inluding ojbConcreteClass
</class>

and

<class-descriptor class="B" table="table">
     // field-descriptors for B including ojbConcreteClass
</class>

The extent-class is inverse to the extends/implements in Java (i.e. 
specified at the supertype), and also note that the class-descriptor for 
B contains all attributes of A (there is no automatic 'inheritance' of 
the descriptors in the repository.xml file) including the 
ojbConcreteClass attribute.

Tom


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


Re: Inheritance: One Table

Posted by LE-QUERE Gildas - REN <gi...@atosorigin.com>.
Hi Tino,
 
Have you an unique Id in class A? Try this example (I have followed the
OJB Tutorial ):
 
 <class-descriptor class="ojb.test.A" table="T_AB"
isolation-level="read-uncommitted" accept-locks="true" refresh="true" >
 
   <extent-class class-ref="ojb.test.B" />
 
         <field-descriptor name="oid" column="oid" jdbc-type="INTEGER"
primarykey="true" autoincrement="true"/>
          
         <field-descriptor name="attA" column="attA"
jdbc-type="VARCHAR"/>
         
         <field-descriptor name="ojbConcreteClass" column="CLASS_NAME"
jdbc-type="VARCHAR"/>
  
 </class-descriptor>
 
 <class-descriptor class="ojb.test.B" table="T_AB"
isolation-level="read-uncommitted" accept-locks="true" refresh="true">
 
         <field-descriptor name="oid" column="oid" jdbc-type="INTEGER"
primarykey="true" autoincrement="true"/>
         
         <field-descriptor name="attA" column="attA"
jdbc-type="VARCHAR"/>
         
         <field-descriptor name="ojbConcreteClass" column="CLASS_NAME"
jdbc-type="VARCHAR"/>
    
         <field-descriptor name="attB" column="attB"
jdbc-type="VARCHAR"/>
 
 </class-descriptor>
 
Below the ORACLE ddl ( modify database type for MySql):
 
create table T_AB ( 
  oid    INTEGER   NOT NULL,
  attA    VARCHAR2 ( 48 )  NULL,
  attB    VARCHAR2 ( 48 )  NULL,
  CLASS_NAME VARCHAR ( 48 )  NULL,
  constraint PK_T_AB PRIMARY KEY (oid)
)

I hope that will help you,
 
Gildas
----- Original Message ----- 
From: "Tino Schöllhorn" <  <ma...@tiscali.de>
t.schoellhorn@tiscali.de>
To: <  <ma...@db.apache.org> ojb-user@db.apache.org>
Sent: Monday, June 14, 2004 8:46 AM
Subject: Inheritance: One Table

> Hi,
> 
> I have I inheritance-problem. I want to map two classes to one table. 
> They are defined as followed:
> 
> class A {
> int id;
> protected String ojbConcreteClass;
> // some attributes 
> }
> 
> class B extends A {
> // some additional attributes
> }
> 
> 
> I created class-descriptors for each of the table:
> 
> <class-descriptor class="A" table="table">
> // field-descriptors for A inluding ojbConcreteClass
> </class>
> 
> and
> 
> 
> <class-descriptor class="B" table="table" extends="A">
> // additional field-descriptors for B
> </class>
> 
> 
> So: When I try to store an instance of Type A everything is fine. But 
> when I try to store an instance of Type B OJB complains:
> 
> Caused by: 
> org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException:
OJB 
> needs at least one primary key attribute for class 
> objectsRealClass=class kos.wnetz.om.RelationHistoryEvent, 
> objectTopLevelClass=class kos.wnetz.om.RelationHistoryEvent
> at
org.apache.ojb.broker.Identity.checkForPrimaryKeys(Identity.java:245)
> at org.apache.ojb.broker.Identity.init(Identity.java:121)
> 
> 
> So I also tried to include the field-descriptors of the superclass in 
> the class-descriptor for class B. But then I get an even worse error:
> 
> Caused by: java.sql.SQLException: Parameter index out of range (8 >
7).
> at 
>
com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:1940
)
> at
com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:1075)
> at
com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:766)
> at
com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:892)
> at 
>
org.apache.ojb.broker.platforms.PlatformDefaultImpl.setObjectForStatemen
t(PlatformDefaultImpl.java:227)
> at 
>
org.apache.ojb.broker.platforms.PlatformMySQLImpl.setObjectForStatement(
PlatformMySQLImpl.java:87)
> at 
>
org.apache.ojb.broker.accesslayer.StatementManager.bindInsert(StatementM
anager.java:449)
> at 
>
org.apache.ojb.broker.accesslayer.JdbcAccessImpl.executeInsert(JdbcAcces
sImpl.java:193)
> ... 47 more
> 
> So obviously I am missing somthing. do you have any suggestions?
> 
> Tino
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:  <ma...@db.apache.org>
ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail:  <ma...@db.apache.org>
ojb-user-help@db.apache.org