[ 
https://issues.apache.org/jira/browse/CAY-2880?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17922401#comment-17922401
 ] 

Nikita Timofeev commented on CAY-2880:
--------------------------------------

Hi [~soliax]. Unfortunately you are struggling with a bit complicated API.

Much more convenient way to add prefetches to the query is via 
{{query.prefetch(String path, int semantics)}} method:
{code:java}
query.prefetch("example.test", PrefetchTreeNode.JOINT_PREFETCH_SEMANTICS)
        .prefetch("example.test2", 
PrefetchTreeNode.DISJOINT_PREFETCH_SEMANTICS);{code}
Alternatively, if you need to initialize all prefetches beforehand (like you 
are using some generic helper methods), you could do it like this:
{code:java}
PrefetchTreeNode root = new PrefetchTreeNode();
PrefetchTreeNode prefetchTreeNode = root.addPath("example.test");
prefetchTreeNode.setSemantics(PrefetchTreeNode.JOINT_PREFETCH_SEMANTICS);
prefetchTreeNode.setPhantom(false);

prefetchTreeNode = root.addPath("example.test2");
prefetchTreeNode.setSemantics(PrefetchTreeNode.DISJOINT_PREFETCH_SEMANTICS);
prefetchTreeNode.setPhantom(false);
query.prefetch(root); {code}
Hope this helps!

> Unable to Set Name for Child Nodes in PrefetchTreeNode
> ------------------------------------------------------
>
>                 Key: CAY-2880
>                 URL: https://issues.apache.org/jira/browse/CAY-2880
>             Project: Cayenne
>          Issue Type: Bug
>          Components: Core Library
>    Affects Versions: 4.2.1
>         Environment: Windows 11 Java 17
>            Reporter: lallemand
>            Priority: Major
>             Fix For: 4.2.2
>
>
> *Description:*
> It is currently impossible to use child nodes for {{PrefetchTreeNode}} 
> because the {{name}} variable cannot be set. This leads to an error when 
> calling {{{}addChild(){}}}, making the intended functionality unusable.
> *Affected Code:*
> {code:java}
> PrefetchTreeNode prefetchTreeNode = new PrefetchTreeNode();
> // Prefetch 1
> PrefetchTreeNode childPrefetchTreeNode = new PrefetchTreeNode();
> childPrefetchTreeNode.addPath("example.test");
> childPrefetchTreeNode.setSemantics(PrefetchTreeNode.JOINT_PREFETCH_SEMANTICS);
> prefetchTreeNode.addChild(childPrefetchTreeNode);
> // Prefetch 2
> childPrefetchTreeNode = new PrefetchTreeNode();
> childPrefetchTreeNode.addPath("example.test2");
> childPrefetchTreeNode.setSemantics(PrefetchTreeNode.DISJOINT_PREFETCH_SEMANTICS);
> prefetchTreeNode.addChild(childPrefetchTreeNode);
> query.prefetch(prefetchTreeNode);
> ObjectContext context = BaseContext.getThreadObjectContext();
> List<?> items = context.performQuery(query); {code}
> *Cayenne* {{*addChild*}} *Function:*
> {code:java}
> public void addChild(PrefetchTreeNode child) {
>     if (Util.isEmptyString(child.getName())) {
>         throw new IllegalArgumentException("Child has no segmentPath: " + 
> child);
>     } else {
>         if (child.getParent() != this) {
>             child.getParent().removeChild(child.getName());
>             child.parent = this;
>         }
>         if (this.children == null) {
>             this.children = new ArrayList(4);
>         }
>         this.children.add(child);
>     }
> } {code}
> *Issue:*
> The {{addChild}} function requires the child node to have a name. However:
>  * The {{PrefetchTreeNode}} constructor that accepts a {{name}} is protected.
>  * The {{name}} variable itself is protected and cannot be accessed 
> externally.
>  * There is no public setter method for {{{}name{}}}.
> As a result, it is impossible to set the name of a child node, making the 
> {{addChild()}} method non-functional for child nodes.
> *Workaround:*
> None available at this time.
> *Suggested Fix:*
>  * Provide a public constructor that allows setting the {{{}name{}}}.
>  * Alternatively, add a public setter method for {{name}} to enable proper 
> initialization of child nodes.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to