# How to persist custom user attributes to the user the profile

**URL:** <https://forum.keycloak.org/t/how-to-persist-custom-user-attributes-to-the-user-the-profile/18946>\
**Category:** Miscellanaeous\
**Created:** [December 13, 2022, 11:49am UTC](https://forum.keycloak.org/t/how-to-persist-custom-user-attributes-to-the-user-the-profile/18946 "2022-12-13T11:49:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dharani](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@Dharani](https://forum.keycloak.org/u/Dharani)\
**Post date:** [December 13, 2022, 11:49am UTC](https://forum.keycloak.org/t/how-to-persist-custom-user-attributes-to-the-user-the-profile/18946/1 "2022-12-13T11:49:48Z")

</div>

I am writing an EventListener SPI within which i am capturing a Register Event .  
Whenever REGISTER event occurs I want to store some custom user attributes for the registered user.

could anyone suggest how do i achieve this.

---

<div class="post-metadata">

**Author:** ![Afea13](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/afea13/32/9994_2.png) [@Afea13](https://forum.keycloak.org/u/Afea13)\
**Post date:** [April 5, 2024, 3:53am UTC](https://forum.keycloak.org/t/how-to-persist-custom-user-attributes-to-the-user-the-profile/18946/2 "2024-04-05T03:53:10Z")

</div>

did you get any solution?

---

<div class="post-metadata">

**Author:** ![xgp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/xgp/32/2589_2.png) [@xgp](https://forum.keycloak.org/u/xgp)\
**Post date:** [April 5, 2024, 7:18am UTC](https://forum.keycloak.org/t/how-to-persist-custom-user-attributes-to-the-user-the-profile/18946/3 "2024-04-05T07:18:44Z")

</div>

Something like this has worked for me:

```auto
      public void onEvent(final Event event) {
        if (EventType.REGISTER.equals(event.getType())) {
          RealmModel realm = session.realms().getRealm(event.getRealmId());
          UserModel user = session.users().getUserById(realm, event.getUserId());
          if (user != null) {
            KeycloakModelUtils.runJobInTransaction(
                session.getKeycloakSessionFactory(),
                s -> {
                  RealmModel r = session.realms().getRealm(event.getRealmId());
                  UserModel u = session.users().getUserById(r, event.getUserId());
                  u.setSingleAttribute("foo", "bar");
                });
          }
        }
      }

```

For some reason, I had trouble getting it to write if I didn’t create a separate transaction. It might also be possible to enlist a transaction on complete of session, but I haven’t tried that.
