I have an entity Account which has a composition of comments. I have created an edit screen with two tabs: one for the account details and one that shows the comments and you can add a comment as well. To add a comment I have a separate datasource. What is the best way of setting the account for the comment datasource based on the account that is being edited?
First, decide whether to use nested or non-nested datasources. Please see this topic.
If you go for non-nested datasources, you should link them (i.e. set the edited account as a a parameter of comments query). You can do it declaratively using “ds$” prefix for the parameter:
<datasource id="customerDs"
class="com.company.sales.entity.Customer"
view="_local"/>
<collectionDatasource id="ordersDs"
class="com.company.sales.entity.Order"
view="_local">
<query>
<![CDATA[select o from sales$Order o where o.customer.id = :ds$customerDs]]>
</query>
</collectionDatasource>
In this case, no code is needed.
Alternatively, you can use a “custom$” parameter prefix and pass the parameter value in the Datasource.refresh() method:
<collectionDatasource id="ordersDs"
class="com.company.sales.entity.Order"
view="_local">
<query>
<![CDATA[select o from sales$Order o where o.customer.id = :custom$customer]]>
</query>
</collectionDatasource>
I understand the concept (or at least I hope) of collection datasources linked to the master entity or using nested datasources as an alternative and this is working for me. What I do not understand is how to link a single datasource (in this case Comment) to add a Comment to the collection. Maybe I can illustrate what I have done so far in the XML below. The intention is when I click the addButton that the comment is added, however the “account” field in the Comment that is associated to the Account is not set. I wonder how I can best set that.
A complete code example would be appreciated as I am still struggling to grasp the concepts, especially if there are multiple data sources involved that is not the main data source of the AbstractEditor.
I think you don’t need a separate datasource for creating a Comment at all. You can directly use a value of the TextField, so the method may look as follows:
That clearly works. Thanks for that. I noticed that if I do a commentsDs.commit() after adding the item the screen still requires a commit which I have added. Is there an alternative to just get the comment that I just added committed without having to do a screen commit?
Probably this is because you have the composition of datasources. When you add an item to the nested datasource, both of them becomes dirty (because the master owns the collection of nested). But you explicitly commit only the nested one. When you close the screen, it warns you about uncommitted changes in master.
If you really don’t need a composition (in CUBA terms) of entities, use separate datasources linked with a parameter as in my first comment. Then when you add an item to the Comments datasource the Account datasource will remain untouched.