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 "Martin I. Levi" <ml...@labtie.mmt.upc.es> on 2004/06/30 13:17:26 UTC

PersistentField class doubt!

Hi!

I have a doubt related to PersistentFields and object mappings...
here i go:

I obtain a PersistentField from an object in this way:
-----------------------code starts here---------------------------------

private void loadFieldValues(BaseBusinessObject object)
throws MetadataException
{
Class attributeType = object.getClass();
MetadataManager mm = MetadataManager.getInstance();
DescriptorRepository dr = mm.copyOfGlobalRepository();
ClassDescriptor cld = dr.getDescriptorFor(attributeType);
FieldDescriptor[] fields = cld.getAllRwFields();
 

for (int i = 0; i < fields.length; i++)
{
//the code inside this "for" shows every field from
//(this) object and it works great so i omit it.
}

Vector references = cld.getObjectReferenceDescriptors();

//the following for creates a recursion
//to show every referenced object
//from this and the following referenced objects, etc.

for (int i = 0; i < references.size(); i++)
{
        ObjectReferenceDescriptor reference =
        (ObjectReferenceDescriptor) references.get(i);
        
        System.out.println("-------------- " +
        reference.getAttributeName()
        + " -------------");
        
        String attributeName = reference.getAttributeName();
        
        PersistentField pf =
        PersistentFieldFactory.createPersistentField(attributeType,attributeName);
        
        BaseBusinessObject objReference = (BaseBusinessObject)
        pf.get(object); 
        
        loadFieldValues(objReference); //recursive call!
}

}
-----------------------code ends here---------------------------------

Problems appear when I use primitive ints in primary keys for my
objects. When a referenced object has a 0 key "pf.get(object)" returns
null and not the object referenced by the 0 key.
When I use Integer objects as IDs I dont have any trouble... can anyone
tell me why this happens?

Thanks!

-- 
Saludos,

Martin I. Levi

Centre Tecnològic de Transferenciència de Calor
Universitat Politècnica de Catalunya
www.cttc.upc.edu


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


Re: PersistentField class doubt!

Posted by Armin Waibel <ar...@apache.org>.
Hi Martin,

 > Problems appear when I use primitive ints in primary keys for my
 > objects. When a referenced object has a 0 key "pf.get(object)" returns
 > null and not the object referenced by the 0 key.
 > When I use Integer objects as IDs I dont have any trouble... can anyone
 > tell me why this happens?

This is expected behavior. The problem with primitive types is that they 
never represent 'null'. So when represents a primitive FK/PK null? When 
is a reference removed when FK is primitive? Assume you have a reference 
from A to B and the primitive PK of B is 0 (thus FK in A is 0), how can 
you reject the reference? The FK in A is already 0?
OJB use the default value of primitive PK,FK fields ('0') as 'null' 
representation. Thus if you use primitive fields as PK, all values 
should be != 0.
This is only true for primitive PK/FK fields. All other fields have 
'normal' behavior.

regards,
Armin


Martin I. Levi wrote:
> Hi!
> 
> I have a doubt related to PersistentFields and object mappings...
> here i go:
> 
> I obtain a PersistentField from an object in this way:
> -----------------------code starts here---------------------------------
> 
> private void loadFieldValues(BaseBusinessObject object)
> throws MetadataException
> {
> Class attributeType = object.getClass();
> MetadataManager mm = MetadataManager.getInstance();
> DescriptorRepository dr = mm.copyOfGlobalRepository();
> ClassDescriptor cld = dr.getDescriptorFor(attributeType);
> FieldDescriptor[] fields = cld.getAllRwFields();
>  
> 
> for (int i = 0; i < fields.length; i++)
> {
> //the code inside this "for" shows every field from
> //(this) object and it works great so i omit it.
> }
> 
> Vector references = cld.getObjectReferenceDescriptors();
> 
> //the following for creates a recursion
> //to show every referenced object
> //from this and the following referenced objects, etc.
> 
> for (int i = 0; i < references.size(); i++)
> {
>         ObjectReferenceDescriptor reference =
>         (ObjectReferenceDescriptor) references.get(i);
>         
>         System.out.println("-------------- " +
>         reference.getAttributeName()
>         + " -------------");
>         
>         String attributeName = reference.getAttributeName();
>         
>         PersistentField pf =
>         PersistentFieldFactory.createPersistentField(attributeType,attributeName);
>         
>         BaseBusinessObject objReference = (BaseBusinessObject)
>         pf.get(object); 
>         
>         loadFieldValues(objReference); //recursive call!
> }
> 
> }
> -----------------------code ends here---------------------------------
> 
> Problems appear when I use primitive ints in primary keys for my
> objects. When a referenced object has a 0 key "pf.get(object)" returns
> null and not the object referenced by the 0 key.
> When I use Integer objects as IDs I dont have any trouble... can anyone
> tell me why this happens?
> 
> Thanks!
> 

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


Re: PersistentField class doubt!

Posted by "Martin I. Levi" <ml...@labtie.mmt.upc.es>.
Thank you Danilo and Armin for your explanations!

About the bug Danilo talks about: I used to have problems it in rc5 with
Integer(0) keys but rc7 seems to work fine for us.

Thanks once again.

-- 
Saludos,

Martin I. Levi

Centre Tecnològic de Transferenciència de Calor
Universitat Politècnica de Catalunya
www.cttc.upc.edu


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


Re: PersistentField class doubt!

Posted by Armin Waibel <ar...@apache.org>.
Danilo Tommasina wrote:

> Hi,
> 
> I guess it is always the same problem with 0 and null keys, there are 
> already several posts about this in the mailing list, I guess that a big 
> red text should be put somewhere in the OJB doc (if it isn't already 
> there) :)
>

It's not there already, but I will put such a 'big red warn' ASAP.
Thanks for the hint.


> The problem is that primitive types cannot be null, so 0 is interpreted 
> by OJB as null for primitive types, for this reason you do not recieve 
> any results when queriing with a primitive 0 primary key, but works 
> correctly if queriing with an Integer( 0 ).
> 
> Also if this may work, you should not use primitive 0 nor Integer( 0 ) 
> as primary key, since OJB (at least in RC4) is/was interpreting also 
> Integer( 0 ) as null when updating objects into db, causing inserts of 
> new objects (PK null) instead of updates (PK = 0). I do not know if this 
> issue has been fixed in RC5 and/or later, sorry.
> 

hmm, this should be fixed. If the problem still exist, please make a bug 
report.

regards,
Armin


> Cheers
> Danilo
> 
> 
>> Hi!
>>
>> I have a doubt related to PersistentFields and object mappings...
>> here i go:
>>
>> I obtain a PersistentField from an object in this way:
>> -----------------------code starts here---------------------------------
>>
>> private void loadFieldValues(BaseBusinessObject object)
>> throws MetadataException
>> {
>> Class attributeType = object.getClass();
>> MetadataManager mm = MetadataManager.getInstance();
>> DescriptorRepository dr = mm.copyOfGlobalRepository();
>> ClassDescriptor cld = dr.getDescriptorFor(attributeType);
>> FieldDescriptor[] fields = cld.getAllRwFields();
>>  
>>
>> for (int i = 0; i < fields.length; i++)
>> {
>> //the code inside this "for" shows every field from
>> //(this) object and it works great so i omit it.
>> }
>>
>> Vector references = cld.getObjectReferenceDescriptors();
>>
>> //the following for creates a recursion
>> //to show every referenced object
>> //from this and the following referenced objects, etc.
>>
>> for (int i = 0; i < references.size(); i++)
>> {
>>         ObjectReferenceDescriptor reference =
>>         (ObjectReferenceDescriptor) references.get(i);
>>                 System.out.println("-------------- " +
>>         reference.getAttributeName()
>>         + " -------------");
>>                 String attributeName = reference.getAttributeName();
>>                 PersistentField pf =
>>         
>> PersistentFieldFactory.createPersistentField(attributeType,attributeName); 
>>
>>                 BaseBusinessObject objReference = (BaseBusinessObject)
>>         pf.get(object);                 loadFieldValues(objReference); 
>> //recursive call!
>> }
>>
>> }
>> -----------------------code ends here---------------------------------
>>
>> Problems appear when I use primitive ints in primary keys for my
>> objects. When a referenced object has a 0 key "pf.get(object)" returns
>> null and not the object referenced by the 0 key.
>> When I use Integer objects as IDs I dont have any trouble... can anyone
>> tell me why this happens?
>>
>> Thanks!
>>
> 
> 
> ---------------------------------------------------------------------
> 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


Re: PersistentField class doubt!

Posted by Danilo Tommasina <dt...@risksys.com>.
Hi,

I guess it is always the same problem with 0 and null keys, there are 
already several posts about this in the mailing list, I guess that a big 
red text should be put somewhere in the OJB doc (if it isn't already 
there) :)

The problem is that primitive types cannot be null, so 0 is interpreted 
by OJB as null for primitive types, for this reason you do not recieve 
any results when queriing with a primitive 0 primary key, but works 
correctly if queriing with an Integer( 0 ).

Also if this may work, you should not use primitive 0 nor Integer( 0 ) 
as primary key, since OJB (at least in RC4) is/was interpreting also 
Integer( 0 ) as null when updating objects into db, causing inserts of 
new objects (PK null) instead of updates (PK = 0). I do not know if this 
issue has been fixed in RC5 and/or later, sorry.

Cheers
Danilo


> Hi!
> 
> I have a doubt related to PersistentFields and object mappings...
> here i go:
> 
> I obtain a PersistentField from an object in this way:
> -----------------------code starts here---------------------------------
> 
> private void loadFieldValues(BaseBusinessObject object)
> throws MetadataException
> {
> Class attributeType = object.getClass();
> MetadataManager mm = MetadataManager.getInstance();
> DescriptorRepository dr = mm.copyOfGlobalRepository();
> ClassDescriptor cld = dr.getDescriptorFor(attributeType);
> FieldDescriptor[] fields = cld.getAllRwFields();
>  
> 
> for (int i = 0; i < fields.length; i++)
> {
> //the code inside this "for" shows every field from
> //(this) object and it works great so i omit it.
> }
> 
> Vector references = cld.getObjectReferenceDescriptors();
> 
> //the following for creates a recursion
> //to show every referenced object
> //from this and the following referenced objects, etc.
> 
> for (int i = 0; i < references.size(); i++)
> {
>         ObjectReferenceDescriptor reference =
>         (ObjectReferenceDescriptor) references.get(i);
>         
>         System.out.println("-------------- " +
>         reference.getAttributeName()
>         + " -------------");
>         
>         String attributeName = reference.getAttributeName();
>         
>         PersistentField pf =
>         PersistentFieldFactory.createPersistentField(attributeType,attributeName);
>         
>         BaseBusinessObject objReference = (BaseBusinessObject)
>         pf.get(object); 
>         
>         loadFieldValues(objReference); //recursive call!
> }
> 
> }
> -----------------------code ends here---------------------------------
> 
> Problems appear when I use primitive ints in primary keys for my
> objects. When a referenced object has a 0 key "pf.get(object)" returns
> null and not the object referenced by the 0 key.
> When I use Integer objects as IDs I dont have any trouble... can anyone
> tell me why this happens?
> 
> Thanks!
> 


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