# Playbook: debug a wrong value

**The question, in the user's words:** "A user's credentials come back in the wrong order. The `PRIORITY` column of the `CREDENTIAL` table looks wrong. What writes it?"

**What you get at the end:** every method that reads, writes or deletes the table, the methods that set the field, and the routes and callers that reach them. Then, for a value that arrives as a message, who publishes it and who handles it.

Words used on this page:

- a **data store** is a table, collection, cache or queue;

- an **ORM** is a library that maps classes to tables, so the code saves an object and never writes the table's name;

- **JPA** is Java's standard ORM, and its **EntityManager** is the object code calls to load, save and delete rows (`em.find`, `em.persist`, `em.remove`);

- a **loaded entity** is an object JPA has read from a row: changing its fields changes the row when the **transaction** (the unit of work JPA saves in one go) ends, with no further call;

- a **named query** is a query written once on the entity class and run by its name (`em.createNamedQuery("credentialByUser", ...)`).

## The steps

### 1. Which stores does the code use?

```
niro ask list_data_resources limit=15

67 data resource(s):
sql          table       USER_ENTITY                  20 use(s)
sql          table       REALM                        10 use(s)
...
sql          table       CREDENTIAL                   7 use(s)

```

### 2. Who reads, writes and deletes that table?

```
niro ask get_data_resource_usage resource_fqn=CREDENTIAL

sql:table:CREDENTIAL
read by (5):
JpaUserCredentialStore.getStoredCredentialById
JpaUserCredentialStore.removeCredentialEntity
JpaUserCredentialStore.updateCredential
CredentialModelTest.testCredentialUpdateWithDuplicateLabel
PasswordHashingTest.testPasswordRehashedOnAlgorithmChangedWithMigratedSalt
written by (1):
JpaUserCredentialStore.createCredentialEntity
deleted by (1):
JpaUserCredentialStore.removeCredentialEntity

```

The listed write is `em.persist(entity)` on a `CredentialEntity`, and the table name appears only once in the code, in `@Table(name="CREDENTIAL")` on the entity class. Niro joins the two.

**This list is not complete, and here is what it leaves out.** The answer key for this same question (A/B task p-04) has four methods that write the table, and step 2 shows one insert and one delete:

- `updateCredential` loads a row with `em.find` and changes its fields, so it writes the row; Niro lists it only under "read by".

- `moveCredentialTo` changes the priority of rows it loaded earlier, and is not listed at all.

- `getStoredCredentialEntities` reads the table through a named query, and is not listed either.

Both kinds of gap are known problems in this release: see [FAQ: wrong or missing answers](https://help.niroai.dev/cli/faq-wrong-or-missing-answers/). Use step 3 to find them.

### 3. Who sets the field?

The graph records which methods touch a table, not which fields they change. So search for the setter, and ask for the methods that hold it:

```
niro ask code_grep pattern="setPriority(" path_pattern=model/jpa/ answer=names

18 matching lines in 9 files
JpaUserCredentialStore.createCredentialEntity(RealmModel, UserModel, CredentialModel)
JpaUserCredentialStore.removeCredentialEntity(RealmModel, UserModel, String)
JpaUserCredentialStore.moveCredentialTo(RealmModel, UserModel, String, String)
RealmAdapter.entityToModel(AuthenticationExecutionEntity)
...
[shown 17 names from the first 5 hits a file; raise max_matches_per_file (up to 50) for more hits a file]

```

Three methods of `JpaUserCredentialStore` set a credential's priority. `moveCredentialTo` is not in step 2's list, and `updateCredential` is listed only as a reader. Both change loaded entities, and the database update happens when the transaction ends. That kind of write has no call Niro can see.

### 4. What reaches the suspect method?

```
niro ask get_impact_analysis method_fqn=org.keycloak.models.jpa.JpaUserCredentialStore.moveCredentialTo depth=5 answer=names

Impact analysis for JpaUserCredentialStore.moveCredentialTo — 5 affected functions, risk: medium (+2 in tests, not counted in risk) (upstream 7)
upstream:
JpaUserProvider.moveCredentialTo (depth 1)
UserCredentialManager.moveStoredCredentialTo (depth 2)
SubjectCredentialManagerCacheAdapter.moveStoredCredentialTo (depth 3)
UserResource.moveCredentialAfter (depth 4)
UserResource.moveCredentialToFirst (depth 5)
CredentialModelTest.testCredentialCRUD [test] (depth 4)
UserStorageTest.testCredentialCRUD [test] (depth 4)

```

So the admin REST methods `UserResource.moveCredentialAfter` and `moveCredentialToFirst` reorder credentials, and two existing tests already reach the code. The path goes through `SubjectCredentialManagerCacheAdapter`, a class that implements an interface: the call site names only the interface.

### 5. When the value arrives as a message

If the wrong value comes from another service, ask who sends and who receives it. From the synthetic java-mono corpus (68 small Java services and a `main` that calls them):

```
niro ask get_event_producers_and_handlers name=price-changes

redis-pubsub:price-changes   [jedis]
published by (1):
BrokerClients.priceChanged
handled by (1):
PriceWatch.listen

```

Then `get_blast_radius event_fqn=price-changes answer=names` lists what the handler goes on to call. For events in general, see [Playbook: data stores and events](https://help.niroai.dev/cli/playbook-data-and-events/).

## Why the answer is complete, and where it is not

- **Step 2 lists the calls Niro recognises** (here `em.persist`, `em.find` and `em.remove`), including access through an ORM where the table is never named. It is not complete: writes made by changing a loaded entity, and reads through a named query, are missing in this release.

- **It does not hold field writes, or SQL that no call runs.** Step 3 fills the field gap with a text search, which is the right tool for a setter name.

- **An event's two sides are matched by the topic name.** A consumer in a form Niro does not read yet is missing. See the known gaps in [FAQ: wrong or missing answers](https://help.niroai.dev/cli/faq-wrong-or-missing-answers/).

## What grep would do instead

- **Searching for the table name finds one line:** `grep -rnw CREDENTIAL --include=*.java` in Keycloak prints only the `@Table` annotation. Every read and write goes through `em.persist`, `em.find` and named queries on the entity class.

- **Searching for the entity class finds 13 files,** and grep cannot tell which of them write and which only read.

## Cost

What these steps cost depends on your code and your question. How Niro counts the size of its answers, and how to measure the cost on your own tasks, is in [Tokens, speed and accuracy](https://help.niroai.dev/cli/tokens-speed-and-accuracy/).

## Related

[Playbook: debug an error message or a stack trace](https://help.niroai.dev/cli/playbook-debug-exception/), [Playbook: data stores and events](https://help.niroai.dev/cli/playbook-data-and-events/), [Get shorter answers](https://help.niroai.dev/cli/answer-shapes/).