Use of enums as an attribute in an entity

Hello

I’m using Jmix 2.5.1

I have an enum as a property of a Sign class:

import io.jmix.core.metamodel.annotation.JmixEntity;
import io.jmix.core.metamodel.annotation.Store;
import io.jmix.data.DdlGeneration;
import jakarta.persistence.*;
import py.com.itti.secure.certificates.backoffice.utils.SignMethods;

import java.util.Date;

@DdlGeneration(value = DdlGeneration.DbScriptGenerationMode.DISABLED)
@JmixEntity
@Store(name = "certificates")
@Table(name = "sign")
@Entity
public class Sign {
    @Column(name = "pk", nullable = false)
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer pk;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at", nullable = false)
    private Date createdAt;

    @Column(name = "METHOD")
    private String method; //enum in the jmix studio entity design
}

Enum for the “method” attribute:

import io.jmix.core.metamodel.datatype.EnumClass;
import lombok.Getter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.lang.NonNull;

@Getter
public enum SignMethods implements EnumClass<String> {
    PIN("PIN", "Pin"),
    SELFIE("SELFIE", "Selfie");

    private final String id;
    private final String label;

    SignMethods(String id, String label) {
        this.id = id;
        this.label = label;
    }

    @Override
    @NonNull
    public String getId() {
        return id;
    }

    public static SignMethods fromId(String id) {
        for (SignMethods status : SignMethods.values()) {
            if (status.getId().equals(id)) {
                return status;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("id", id)
                .toString();
    }
}

Where the attribute method is an enum, according to Jmix design, as a data type.
The thing is that it is recognized as an enum — in the filter it appears as a dropdown, but there (and in the datagrid display, in the detail page, and when exporting to a file using Grid Export Action), the value shown is SignMethods.PIN and not just PIN.
Is that normal? Is it not possible to only show the value (like PIN), and not the full enum representation (like SignMethods.PIN)?
Looking forward to your comments. Thank you very much!

Hi Carlos
Do you have a proper set of messages for this enum?
Like com.company.entity.SignMethods.PIN=Pin ?

Regards,
Konstantin

I just tried it and it doesn’t work — the filter displays it like this, and it also shows the same way when exporting the data to Excel.

image
image

What locale is used in your project?
Are you sure that the corresponding messages_XX.properties file contains the messages for the enum values?

Hello,

Yes, I’m sure. I’ve tested with the following:

SignMethods.PIN=Pin  
SignMethods.SELFIE=Selfie

And also like this:

py.com.itti.secure.certificates.backoffice.utils.SignMethods.PIN=PIN  
py.com.itti.secure.certificates.backoffice.utils.SignMethods.SELFIE=SELFIE

The file is messages_es_PY.properties, for Spanish, and it’s working — the system is getting translations from it for everything else. I’m only using one language.

Here is my enum file:

package py.com.itti.secure.certificates.backoffice.utils;

import io.jmix.core.metamodel.datatype.EnumClass;
import lombok.Getter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.lang.NonNull;

@Getter
public enum SignMethods implements EnumClass<String> {
    PIN("PIN", "Pin"),
    SELFIE("SELFIE", "Selfie");

    private final String id;
    private final String label;

    SignMethods(String id, String label) {
        this.id = id;
        this.label = label;
    }

    @Override
    @NonNull
    public String getId() {
        return id;
    }

    public static SignMethods fromId(String id) {
        for (SignMethods status : SignMethods.values()) {
            if (status.getId().equals(id)) {
                return status;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("id", id)
                .toString();
    }
}

Both are wrong.
Should be py.com.itti.secure.certificates.backoffice.utils/SignMethods.PIN=PIN

You can generate/edit localized messages in the enum designer by clicking on the “globe” button:

image

Regards,
Konstantin

Thank you very much for your response — you’re absolutely right, the way to add the translation is exactly as you showed. I had tried using the design tab to add the translation, but apparently it doesn’t work if the enum has an extra parameter in its constructor besides the ID, which I’ve confirmed. It’s probably a bug in the Jmix plugin. I removed the label attribute from the enum, and after that I was able to generate the message for the local language file. Thank you so much for your help!
image