8000 JPA Map Storage should preserve order of attributes · Issue #9712 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

JPA Map Storage should preserve order of attributes #9712

Closed as not planned
Closed as not planned

Description

@ahus1

Describe the bug

The Map storage interface defines attributes to be multi-valued and the values should be ordered:

public interface EntityWithAttributes {
    Map<String, List<String>> getAttributes();
    /* ... */
}

The JPA Map implementation stores the attributes as a Set in JpaClientEntity and JpaRoleEntity. This is not preserving the order of attributes.

Version

17.0.0-SNAPSHOT

Expected behavior

  • Multi-valued attribute values should be ordered when stored and retrieved from Map JPA storage.
  • Multiple entries of the same value (that are equal()) should be possible

Actual behavior

  • Order of Multi-valued attribute values is not preserved
  • Multiple entries of the same value (that are equal()) are reduced to a single value

How to Reproduce?

It the characteristics of a Set vs. List that are described above.
The API for multi-valued attributes is currently not exposed for Clients, only for Roles.

Anything else?

There is a GitHub discussion here: #9638

The client_attribute and role_attribute table would need to have an additional column that preserves the ordering of the values, for example via @OrderColumn(name = "index_id").

See https://vladmihalcea.com/how-to-optimize-unidirectional-collections-with-jpa-and-hibernate/ for examples.

This column should have a unique deferrable constraint to avoid DB inconsistencies. It would need to be deferrable to allow Hibernate a re-ordering of the IDs during a transaction.

Note:

If all attributes are stored as one ordered list, changing an attribute by removing it from the list and adding it at the end would change the position of all list items in between, therefore issuing an update on all other attributes in between. An in-place update of especially single-valued attributes in the list would be preferred.

This approach would only be viable if the number of entries in the attributes table is relatively small and the order / position of list items wouldn't change so often.

Activity

  1. added
    kind/bugCategorizes a PR related to a bug
    area/storageStorage layer — JPA entities, database schemas, Liquibase migrations, caching
    on Jan 21, 2022
  2. moved this to Todo in Storageon Jan 21, 2022
  3. ahus1 commented on Jan 24, 2022

    @ahus1
    MemberAuthor

    @vramik noticed that the current JPA implementation doesn't support ordering of multi-valued attributes.

    For example: RoleModel already support multi-valued attributes. RoleEntity contains a List of attributes but no order column. Therefore, it would allow multiple values that are equal per key, but it wouldn't support the order of the attributes.
    Therefore, the current implementation is that of a bag (or idbag to be more specific). Hibernate support bags; as Java doesn't have a bag collection type, the correct Java type is still a List.

    Until this has been clarified, this issue is on hold. There will be soon a new GitHub discussion on this.

  4. ahus1 commented on Jan 26, 2022

    @ahus1
    MemberAuthor

    The discussion is now available: #9794

  5. hmlnarik commented on Feb 16, 2022

    @hmlnarik
    Contributor

    Rejecting this issue as per #9794 there seems no interest in getting the attributes in the right order, and there will be change in the method signatures in this respect

  6. Repository owner moved this from Todo to Done in Storageon Feb 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area/storageStorage layer — JPA entities, database schemas, Liquibase migrations, cachingkind/bugCategorizes a PR related to a bug

Type

No type

Fields

No fields configured for issues without a type.

Projects

  • Status
    ✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

0