Skip to content

Support custom Enums mappings #1077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mipo256 opened this issue Nov 3, 2021 · 4 comments
Closed

Support custom Enums mappings #1077

mipo256 opened this issue Nov 3, 2021 · 4 comments
Assignees
Labels
type: enhancement A general enhancement

Comments

@mipo256
Copy link
Contributor

mipo256 commented Nov 3, 2021

Assume I have some model, which I want to persist to the database.

public class Example {
    Status stats;
}

where Status is an enum, that looks like this:

public enum Status {
    ACTIVE(1),
    BLOCKED(2),
    PENDING(3);
    private int statusCode; 
    Status(int statusCode) {
        this.statusCode = statusCode;
    }
}

The official documentation states:

Enums get mapped to their name

But what if we need to map it into our custom value, like, in our example, we need to map it into the integer code, not the Enum element name itself. Is there a way to do it?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 3, 2021
@schauder schauder added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 8, 2021
@schauder
Copy link
Contributor

Related #787

@ctailor2 ctailor2 self-assigned this Jan 25, 2022
@ctailor2
Copy link
Contributor

I attempted to reproduce the issue by adding an Enum type with a int value to my entity and registering a @WritingConverter and @ReadingConverter to convert it to and from the numeric database type. The converters appear to be working as expected and my entity's Enum typed field is persisted and reloaded appropriately.

If the use case you are seeing this issue on is different and cannot be solved using converters, please provide a Minimimal Reproducable Example, preferable as a Github repository. Make sure to include the database, either as an in memory database or if that is not possible using Testcontainers.

@ctailor2 ctailor2 added the status: waiting-for-feedback We need additional information before we can continue label Jan 25, 2022
@spring-projects-issues
Copy link

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Feb 1, 2022
@spring-projects-issues
Copy link

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Feb 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants