You are viewing a plain text version of this content. The canonical link for it is here.
Posted to graffito-dev@incubator.apache.org by Ruchi Goel <Ru...@Sun.COM> on 2006/09/14 11:05:32 UTC

problem in querying nodes (Discriminator ?) Help needed !

Hi,
   I am trying a simple save and then query for a java object via 
jcrmapping.
I ahve two questions :

1. What is the concept of "discriminator " and when is it set to 
true/false ?

2. I have set discriminator = falsein mapping file.In a successive save 
and read , I am able to save a node via the following :

Ad ad = new Ad();
     ad.setPath("/ad");
     ad.setTitle("IT");
     ad.setText("This ad is for IT jobs");
     
ad.setImageAsLink("http://http://incubator.apache.org/images/apache-incubator-logo.png");
     Collection links = new ArrayList();
     links.add((String)"http://yahoo.com");
     links.add((String)"http://google.com");
     ad.setLinks(links);
     try{
        FileInputStream fis = new FileInputStream("c:/tmp/Sun_logo.gif");
        ad.setImageAsBinary(fis);
     }catch(IOException ioe){
         System.out.println(ioe.getMessage());
         System.exit(0);
     }
       pm.insert(ad);
        pm.save();
, but when I try to read it back via following :
   public Ad getAd(String title){
        Filter filter = qm.createFilter(Ad.class);
        filter.addEqualTo("title", title);
        Query query = qm.createQuery(filter);
        Ad ad = (Ad)pm.getObject(query);
        return ad;
    };

The result  says :
Exception in thread "main" 
org.apache.portals.graffito.jcr.exception.PersistenceException: 
Impossible to get the object - the query returns more than one object
        at 
org.apache.portals.graffito.jcr.persistence.impl.PersistenceManagerImpl.getObject(PersistenceManagerImpl.java:454)
        at 
com.sun.portal.cms.service.AdModelServiceImpl.getAd(AdModelServiceImpl.java:54)
        at com.sun.portal.cms.Test.testReadAd(Test.java:57)
        at com.sun.portal.cms.Test.main(Test.java:69)

while I check in the repository, I have exactly one node with this title.

If I try to read the collection of objects ,
I get the following error :
Exception in thread "main" 
org.apache.portals.graffito.jcr.exception.JcrMappingException: 
Impossible to find the classdescriptor for /ad. There is no 
discriminator and associated  JCR node type
        at 
org.apache.portals.graffito.jcr.persistence.objectconverter.impl.ObjectConverterImpl.getObject(ObjectConverterImpl.java:271)
        at 
org.apache.portals.graffito.jcr.persistence.impl.PersistenceManagerImpl.getObjects(PersistenceManagerImpl.java:492)
        at 
com.sun.portal.cms.service.AdModelServiceImpl.getAd(AdModelServiceImpl.java:55)
        at com.sun.portal.cms.Test.testReadAd(Test.java:57)
        at com.sun.portal.cms.Test.main(Test.java:69)
Java Result: 1

I am sure I am missing something here , but cannot trace it . I am able 
to do teh above operations via directly using JSR170 calls.
Any help appreciated.


Thanks,
Ruchi


Re: problem in querying nodes (Discriminator ?) Help needed !

Posted by ruchi goel <Ru...@Sun.COM>.
Hi Christophe,
Thanks for responding. When will be the case when u would like to use 
same JCRNodeType for the entire class heirrachy ? I see in the tests, in 
most heirrachy cases ,  each class in heirrachy is mapped to a different 
custom nodetype, right.

I am using this mapping file :
<graffito-jcr>
<class-descriptor className="com.sun.portal.cms.model.Ad" 
jcrNodeType="nt:unstructured" discriminator="false" >
<!-- Field-descriptor is used to map simple attributes to jcr property -->
    <field-descriptor fieldName="path" path="true" />
    <field-descriptor fieldName="title" jcrName="title" />
    <field-descriptor fieldName="text" jcrName="text" />
    <field-descriptor fieldName="imageAsBinary" jcrName="imageAsBinary" />
    <field-descriptor fieldName="imageAsLink" jcrName="imageAsLink" />
    <collection-descriptor fieldName="links" jcrName="links" 
elementClassName="java.lang.String" 
collectionConverter="org.apache.portals.graffito.jcr.persistence.collectionconverter.impl.MultiValueCollectionConverterImpl" 
/>
 />
</class-descriptor>
</graffito-jcr>


This is for mapping simple java class  (without any heirrachies.) to jcr 
node.


After saving Ad object, I am able to get it back via passing the path 
for that node.
Ad ad = (Ad)pm.getObject("/ad");

but when I pass title as filter and try to search for ad,
   public Ad getAd(String title){
        Filter filter = qm.createFilter(Ad.class);
        filter.addEqualTo("title", title);
        Query query = qm.createQuery(filter);
        Ad ad = (Ad)pm.getObject(query);
        return ad;
    };

it does not let me do.
 What am I missing ?
I ran the tests and they work fine.

 I am also evaluating the versioning  of nodes. Checked this with  
jsr170 calls , but now wish to do the same using OCM . Is versioning not 
possible for the above mentioned node , which  does not have a custom 
node type.

I see this being used in tests only for custom node types  like 
graffito:document  where isMixin="true"

Thanks in advance,
Ruchi


Christophe Lombart wrote:

> Hi Ruchi,
>
> If you want to use the same JCR node type for the complete class
> hierarchy, you have to specify discriminator = true. By this way, the
> persistence manager will add a new property which will contain the
> object class. This is technique is often used in ORM.
> This property is used to instantiate correctly the object.
>
> If you set the discriminator = false, it means that your have one
> different JCR node type per class for the complete hierarchy. That
> means the discriminator is not necessary. Thanks to the jcr node type
> it is possible to retrieve the right java class.
>
> Are you using JCR custom node types ? You can send me your OCM mapping 
> file.
> There are some examples in the unit tests.
>
>
>
> On 9/14/06, Ruchi Goel <Ru...@sun.com> wrote:
>
>> Hi,
>>    I am trying a simple save and then query for a java object via
>> jcrmapping.
>> I ahve two questions :
>>
>> 1. What is the concept of "discriminator " and when is it set to
>> true/false ?
>>
>> 2. I have set discriminator = falsein mapping file.In a successive save
>> and read , I am able to save a node via the following :
>>
>> Ad ad = new Ad();
>>      ad.setPath("/ad");
>>      ad.setTitle("IT");
>>      ad.setText("This ad is for IT jobs");
>>
>> ad.setImageAsLink("http://http://incubator.apache.org/images/apache-incubator-logo.png"); 
>>
>>      Collection links = new ArrayList();
>>      links.add((String)"http://yahoo.com");
>>      links.add((String)"http://google.com");
>>      ad.setLinks(links);
>>      try{
>>         FileInputStream fis = new 
>> FileInputStream("c:/tmp/Sun_logo.gif");
>>         ad.setImageAsBinary(fis);
>>      }catch(IOException ioe){
>>          System.out.println(ioe.getMessage());
>>          System.exit(0);
>>      }
>>        pm.insert(ad);
>>         pm.save();
>> , but when I try to read it back via following :
>>    public Ad getAd(String title){
>>         Filter filter = qm.createFilter(Ad.class);
>>         filter.addEqualTo("title", title);
>>         Query query = qm.createQuery(filter);
>>         Ad ad = (Ad)pm.getObject(query);
>>         return ad;
>>     };
>>
>> The result  says :
>> Exception in thread "main"
>> org.apache.portals.graffito.jcr.exception.PersistenceException:
>> Impossible to get the object - the query returns more than one object
>>         at
>> org.apache.portals.graffito.jcr.persistence.impl.PersistenceManagerImpl.getObject(PersistenceManagerImpl.java:454) 
>>
>>         at
>> com.sun.portal.cms.service.AdModelServiceImpl.getAd(AdModelServiceImpl.java:54) 
>>
>>         at com.sun.portal.cms.Test.testReadAd(Test.java:57)
>>         at com.sun.portal.cms.Test.main(Test.java:69)
>>
>> while I check in the repository, I have exactly one node with this 
>> title.
>>
>> If I try to read the collection of objects ,
>> I get the following error :
>> Exception in thread "main"
>> org.apache.portals.graffito.jcr.exception.JcrMappingException:
>> Impossible to find the classdescriptor for /ad. There is no
>> discriminator and associated  JCR node type
>>         at
>> org.apache.portals.graffito.jcr.persistence.objectconverter.impl.ObjectConverterImpl.getObject(ObjectConverterImpl.java:271) 
>>
>>         at
>> org.apache.portals.graffito.jcr.persistence.impl.PersistenceManagerImpl.getObjects(PersistenceManagerImpl.java:492) 
>>
>>         at
>> com.sun.portal.cms.service.AdModelServiceImpl.getAd(AdModelServiceImpl.java:55) 
>>
>>         at com.sun.portal.cms.Test.testReadAd(Test.java:57)
>>         at com.sun.portal.cms.Test.main(Test.java:69)
>> Java Result: 1
>>
>> I am sure I am missing something here , but cannot trace it . I am able
>> to do teh above operations via directly using JSR170 calls.
>> Any help appreciated.
>>
>>
>> Thanks,
>> Ruchi
>>
>>
>>
>
>


Re: problem in querying nodes (Discriminator ?) Help needed !

Posted by Christophe Lombart <ch...@gmail.com>.
Hi Ruchi,

If you want to use the same JCR node type for the complete class
hierarchy, you have to specify discriminator = true. By this way, the
persistence manager will add a new property which will contain the
object class. This is technique is often used in ORM.
This property is used to instantiate correctly the object.

If you set the discriminator = false, it means that your have one
different JCR node type per class for the complete hierarchy. That
means the discriminator is not necessary. Thanks to the jcr node type
it is possible to retrieve the right java class.

Are you using JCR custom node types ? You can send me your OCM mapping file.
There are some examples in the unit tests.



On 9/14/06, Ruchi Goel <Ru...@sun.com> wrote:
> Hi,
>    I am trying a simple save and then query for a java object via
> jcrmapping.
> I ahve two questions :
>
> 1. What is the concept of "discriminator " and when is it set to
> true/false ?
>
> 2. I have set discriminator = falsein mapping file.In a successive save
> and read , I am able to save a node via the following :
>
> Ad ad = new Ad();
>      ad.setPath("/ad");
>      ad.setTitle("IT");
>      ad.setText("This ad is for IT jobs");
>
> ad.setImageAsLink("http://http://incubator.apache.org/images/apache-incubator-logo.png");
>      Collection links = new ArrayList();
>      links.add((String)"http://yahoo.com");
>      links.add((String)"http://google.com");
>      ad.setLinks(links);
>      try{
>         FileInputStream fis = new FileInputStream("c:/tmp/Sun_logo.gif");
>         ad.setImageAsBinary(fis);
>      }catch(IOException ioe){
>          System.out.println(ioe.getMessage());
>          System.exit(0);
>      }
>        pm.insert(ad);
>         pm.save();
> , but when I try to read it back via following :
>    public Ad getAd(String title){
>         Filter filter = qm.createFilter(Ad.class);
>         filter.addEqualTo("title", title);
>         Query query = qm.createQuery(filter);
>         Ad ad = (Ad)pm.getObject(query);
>         return ad;
>     };
>
> The result  says :
> Exception in thread "main"
> org.apache.portals.graffito.jcr.exception.PersistenceException:
> Impossible to get the object - the query returns more than one object
>         at
> org.apache.portals.graffito.jcr.persistence.impl.PersistenceManagerImpl.getObject(PersistenceManagerImpl.java:454)
>         at
> com.sun.portal.cms.service.AdModelServiceImpl.getAd(AdModelServiceImpl.java:54)
>         at com.sun.portal.cms.Test.testReadAd(Test.java:57)
>         at com.sun.portal.cms.Test.main(Test.java:69)
>
> while I check in the repository, I have exactly one node with this title.
>
> If I try to read the collection of objects ,
> I get the following error :
> Exception in thread "main"
> org.apache.portals.graffito.jcr.exception.JcrMappingException:
> Impossible to find the classdescriptor for /ad. There is no
> discriminator and associated  JCR node type
>         at
> org.apache.portals.graffito.jcr.persistence.objectconverter.impl.ObjectConverterImpl.getObject(ObjectConverterImpl.java:271)
>         at
> org.apache.portals.graffito.jcr.persistence.impl.PersistenceManagerImpl.getObjects(PersistenceManagerImpl.java:492)
>         at
> com.sun.portal.cms.service.AdModelServiceImpl.getAd(AdModelServiceImpl.java:55)
>         at com.sun.portal.cms.Test.testReadAd(Test.java:57)
>         at com.sun.portal.cms.Test.main(Test.java:69)
> Java Result: 1
>
> I am sure I am missing something here , but cannot trace it . I am able
> to do teh above operations via directly using JSR170 calls.
> Any help appreciated.
>
>
> Thanks,
> Ruchi
>
>
>


-- 
Best regards,

Christophe