Problem with Enum on sorted list
sandman202 Jan 18, 2009 4:17 PMI have an entity called Code.java that has a column defined as Enumerated.
Code.java
@Entity public class Code implements Serializable { private CodeType type; ... @Enumerated public CodeType getType() { return type; } public void setType(CodeType type) { this.type = type; } ... }
and CodeType.java
public enum CodeType { MEDIUM("Medium"), EDITION("Edition"); private String value; private CodeType(String value) { this.value = value; } public String getValue() { return value; } }
Inside the CodeList.xhtml, the datatable looks like:
<rich:dataTable id="codeList" var="_code" value="#{codeList.resultList}" rendered="#{not empty codeList.resultList}"> <h:column> <f:facet name="header"> <a4j:htmlCommandLink styleClass="columnHeader" reRender="codeList" value="Type #{codeList.order=='c.type asc' ? messages.down : ( codeList.order=='c.type desc' ? messages.up : '' )}"> <f:param name="codeListOrder" value="#{codeList.order=='c.type asc' ? 'c.type desc' : 'c.type asc'}"/> </a4j:htmlCommandLink> </f:facet> #{_code.type.value} </h:column> ... </rich:dataTable>
And in the CodeList.java:
public class CodeList extends EntityQuery<Item> { private static final long serialVersionUID = 1008397397027179231L; private static final String[] RESTRICTIONS = { "code.type = #{codeList.c.type}", "lower(code.code) like concat(lower(#{codeList.c.code}),'%')", "lower(code.description) like concat(lower(#{codeList.c.description}),'%')", "code.active = #{codeList.c.active}", //"code.strActive = #{codeList.code.strActive}", }; private Code c = new Code(); public CodeList() { this.setMaxResults(25); this.setEjbql("select c from Code c"); this.setOrder("c.type asc"); this.setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS)); } public Code getC() { return c; } public CodeType[] getCodeTypes() { return CodeType.values(); } public BooleanType[] getBooleanTypes() { return BooleanType.values(); } @SuppressWarnings("unchecked") public List<Code> getEditionCodes() { return this.getEntityManager().createNamedQuery("c.findCodesByType") .setParameter("type", CodeType.EDITION).getResultList(); } @SuppressWarnings("unchecked") public List<Code> getMediumCodes() { return this.getEntityManager().createNamedQuery("c.findCodesByType") .setParameter("type", CodeType.MEDIUM).getResultList(); } @Override public String getOrder() { if (super.getOrder() == null) { setOrder("c.type asc"); } return super.getOrder(); } }
The problem is when I try to sort on the enum. It is sorting based on the ordinal value and not the string value. I want to store the ordinal value within the database. Is there a way I can do this?