0

I am facing problem in converting java enum to string conversion. Please if you have any idea.

String mainQuerySt = "select o.ttType from Tt o";

Query mainQuery = em.createQuery(mainQuerySt);
List result = mainQuery.setFirstResult(offset).setMaxResults(numofRecords).getResultList();

I want the string representation of ttType enum. How to do it?

My Tt Definition:

@Enumerated(EnumType.ORDINAL)
@Column(name = "tt_type", nullable = false)
private TTType ttType;   

My enum TTType definition:

public enum TTType
{
    FC,
    PD
    ;

    @Override
    public String toString()
    {
        switch (this)
        {
            case FC:
                return "FC";
            case PD:
                return "PD";
            default:
                throw new AssertionError();
        }
    }
}

I can't use EnumType.STRING in @Enumerated now since the system is live.

Please reply.

4
  • So you dont want to change value at Database level thats why avoiding EnumType.STRING ? Commented Sep 24, 2016 at 12:01
  • @rev_dihazum: yes, since it will require data migration and I want to avoid it. Thanks Commented Sep 24, 2016 at 12:01
  • your String toString() method is also failed to serve purpose? I guess it should work. Commented Sep 24, 2016 at 12:03
  • No, it didn't work. Result contains TtType instead of string representation which I want. Commented Sep 25, 2016 at 0:19

1 Answer 1

1

One option may be to use enum constructor as bellow:

public enum TTType {
    FC("FC"),
    PD("PD");

    private String value; // the value

    // Constructor 
    TTType(String value) {
        this.value = value;
    }

    public String getValue() {
        return this.value;
    }

    public String toString() {
        return this.value;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.