FetchByKey in Java SDK?
- Jean_SafarOP 1
Hi,
Apologies for the trivial question, but I can’t seem to find a java equivalent to fetchByKey of a template neither in the code generated nor in the SDK itself. Is there way to do this?
Best regards
- cocreature 2
The Java bindings just reflect the gRPC Ledger API here so they abide by the same limitations as e.g. DAML Script. For command submissions you have 4 different commands:
createexercisecreateAndExerciseexerciseByKey
fetchByKeyis not one of them so you cannot call it directly. What you can do is to define a helper template and call that viacreateAndExercise:template T with p : Party v : Int where signatory p key (p, v): TKeyType maintainer key._1 type TKeyType = (Party, Int) template Helper with p : Party where signatory p choice FetchByKey : T with key : TKeyType controller p do fetchByKey @T keyAlternatively you can query the ACS endpoint and filter by key on the client side.
- Jean_Safar 3
Thanks a lot @cocreature!
this should however be fixed no? Should it really be the app developer to go and write helper functions for such a common task?
- entzik 3
Writing a template for that is indeed not a practical solution.
While it solves the issue, it is, as @Jean_Safar mentioned, a waste of developer time, it adds to project complexity and maintenance cost, it’s a waste of ledger storage and it takes a percentage out of the actual transaction throughput as we have to issue two transactions in order to perform what is essentially a read on the ledger.
Not to talk about the fact that conceptually, a read on any system should have no side effects on the state of that system, and with this solution, a contract is created and archived so side effect there is.
+1 to add the capability in the gRPC API.
- Jean_Safar 3
+1 but @entzik, it is a work around that is wasteful not a solution !

Merry Christmas and Happy New Year to all !!!