<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:51:31 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[JAVA-102] No mechanism to specify object class for DBObjects returned from DBRef.fetch()</title>
                <link>https://jira.mongodb.org/browse/JAVA-102</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;This is borderline bug / feature request.&lt;/p&gt;

&lt;p&gt;I have been happily using the DBCollection.setObjectClass(CUSTOM_CLASS) feature in my app, but unfortunately, any time I have a DBRef from one DBObject to another, I cannot direct DBRef.fetch() (actually it is DBRefBase.fetch()) to use a custom DBObject class. It always uses BasicDBObject. &lt;/p&gt;

&lt;p&gt;One solution would be to have an optional property of DBRef to configure an object class. If none is specified the default behavior is to use BasicDBObject.&lt;/p&gt;

&lt;p&gt;Current impl code snippet (from github):&lt;/p&gt;

&lt;p&gt;public DBObject fetch() {&lt;br/&gt;
        if (_loadedPointedTo)&lt;br/&gt;
            return _pointedTo;&lt;/p&gt;

&lt;p&gt;        if (_db == null)&lt;br/&gt;
            throw new RuntimeException(&quot;no db&quot;);&lt;/p&gt;

&lt;p&gt;        if (D) &lt;/p&gt;
{
            System.out.println(&quot;following db pointer. ref to ns:&quot; + _ns);
            Throwable t = new Throwable();
            t.fillInStackTrace();
            t.printStackTrace();
        }

&lt;p&gt;        final DBCollection coll = _db.getCollectionFromString(_ns);&lt;/p&gt;

&lt;p&gt;        _pointedTo = coll.findOne(_id);&lt;br/&gt;
        _loadedPointedTo = true;&lt;br/&gt;
        return _pointedTo;&lt;br/&gt;
    }&lt;/p&gt;</description>
                <environment></environment>
        <key id="11754">JAVA-102</key>
            <summary>No mechanism to specify object class for DBObjects returned from DBRef.fetch()</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="9">Done</resolution>
                                        <assignee username="eliot">Eliot Horowitz</assignee>
                                    <reporter username="william.shulman@gmail.com">William Shulman</reporter>
                        <labels>
                    </labels>
                <created>Mon, 12 Apr 2010 00:57:57 +0000</created>
                <updated>Mon, 12 Apr 2010 15:34:07 +0000</updated>
                            <resolved>Mon, 12 Apr 2010 15:34:07 +0000</resolved>
                                                                                        <votes>0</votes>
                                    <watches>0</watches>
                                                                                                                <comments>
                            <comment id="13559" author="william.shulman@gmail.com" created="Mon, 12 Apr 2010 15:28:13 +0000"  >&lt;p&gt;Got it! Thank Eliot. &lt;/p&gt;

&lt;p&gt;I thought that I had initially done as you suggested and properly configured the collection in question with my custom object class, but it turns out that the DBRef.fetch() was indirectly making use of that collection before such configuration could take place. I am now configuring all my collections with their respective custom object classes at system startup (rather than lazily as I had been doing) and everything is working as expected. &lt;/p&gt;

&lt;p&gt;All makes good sense. Thanks again. You can close/resolve (I would do it but I dont have the right permissions).&lt;/p&gt;</comment>
                            <comment id="13535" author="eliot" created="Mon, 12 Apr 2010 10:18:59 +0000"  >&lt;p&gt;I don&apos;t think thats the right place to put it.&lt;/p&gt;

&lt;p&gt;If you set it on the linked collection, it should work as is correctly.&lt;br/&gt;
Can you try that?&lt;/p&gt;</comment>
                            <comment id="13531" author="william.shulman@gmail.com" created="Mon, 12 Apr 2010 01:19:44 +0000"  >&lt;p&gt;I now realize that it is more complicated than this. You need to serialize the new objectClass field when you persist the ref. We dont want our json representation for dbrefs to know about and be coupled to this feature of the Java driver, that would be bad. One solution here might be to add a &apos;meta&apos; field in dbref json documents that hold whatever the client wants it to, and drivers pass it along uninterpreted. Then the Java driver can stuff this objectClass info in there but it wont be semantically coupled to the Java driver, it will just be some opaque meta info as far as the system is concerned.&lt;/p&gt;

&lt;p&gt;Might be some more gotchas as well, I&apos;m new to mongodb&lt;/p&gt;</comment>
                            <comment id="13530" author="william.shulman@gmail.com" created="Mon, 12 Apr 2010 01:01:57 +0000"  >&lt;p&gt;Here is an impl of the solution I mention above (there may be other, better solutions):&lt;/p&gt;

&lt;p&gt;package com.mongodb;&lt;/p&gt;

&lt;p&gt;/**&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Base class for DBRefs.&lt;br/&gt;
 */&lt;br/&gt;
public class DBRefBase {&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;    static final boolean D = Boolean.getBoolean( &quot;DEBUG.DBREF&quot; );&lt;/p&gt;

&lt;p&gt;    public DBRefBase(DB db , String ns , Object id) &lt;/p&gt;
{
         this(db, ns, id, null);
    }
&lt;p&gt;    public DBRefBase(DB db , String ns , Object id, Class objectClass) &lt;/p&gt;
{
        _db = db;
        
        _ns = ns;
        _id = id;

         _objectClass = objectClass;
    }

&lt;p&gt;    public DBObject fetch() {&lt;br/&gt;
        if (_loadedPointedTo)&lt;br/&gt;
            return _pointedTo;&lt;/p&gt;

&lt;p&gt;        if (_db == null)&lt;br/&gt;
            throw new RuntimeException(&quot;no db&quot;);&lt;/p&gt;

&lt;p&gt;        if (D) &lt;/p&gt;
{
            System.out.println(&quot;following db pointer. ref to ns:&quot; + _ns);
            Throwable t = new Throwable();
            t.fillInStackTrace();
            t.printStackTrace();
        }

&lt;p&gt;        final DBCollection coll = _db.getCollectionFromString(_ns);&lt;br/&gt;
        coll.setObjectClass(_objectClass);&lt;/p&gt;

&lt;p&gt;        _pointedTo = coll.findOne(_id);&lt;br/&gt;
        _loadedPointedTo = true;&lt;br/&gt;
        return _pointedTo;&lt;br/&gt;
    }&lt;/p&gt;

&lt;p&gt;    public String toString(){&lt;br/&gt;
        return &quot;&lt;/p&gt;
{ \&quot;$ref\&quot; : \&quot;&quot; + _ns + &quot;\&quot;, \&quot;$id\&quot; : \&quot;&quot; + _id + &quot;\&quot; }
&lt;p&gt;&quot;;&lt;br/&gt;
    }&lt;/p&gt;

&lt;p&gt;    /**&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Get this ref&apos;s $id.&lt;br/&gt;
     */&lt;br/&gt;
    public Object getId() 
{
        return _id;
    }&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;    /**&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Get this ref&apos;s $ref (the collection name).&lt;br/&gt;
     */&lt;br/&gt;
    public String getRef() 
{
        return _ns;
    }&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;    /**&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Get this ref&apos;s database.&lt;br/&gt;
     */&lt;br/&gt;
    public DB getDB() 
{
        return _db;
    }&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;    final Object _id;&lt;br/&gt;
    final String _ns;&lt;br/&gt;
    final DB _db;&lt;br/&gt;
    final Class _objectClass;&lt;/p&gt;

&lt;p&gt;    private boolean _loadedPointedTo = false;&lt;br/&gt;
    private DBObject _pointedTo;&lt;br/&gt;
}&lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hrhd7r:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>14941</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>