Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>So I'm thinking of switching to EnumType.STRING to not have to remap ordinal values in the database again. But if I do this, then how do I sort properly? The alphabetical order of the enum strings is not the order I need.</p> </blockquote> <p>I personally totally avoid using the evil <code>EnumType.ORDINAL</code> as just changing the order of the constants would brake the persistence logic. Evil. That said, <code>EnumType.STRING</code> is indeed not always appropriate. </p> <p>In your case, here is what I would do (with standard JPA): I would persist an <code>int</code> at the entity level and perform the enum conversion in getter/setters. Something like this. First, the enum:</p> <pre><code>public enum Status { PLANNING("Planning", 100), DEVELOPMENT("Development", 200), PRODUCTION("Production", 300); private final String label; private final int code; private Status(String label, int code) { this.label = label; this.code = code; } public int getCode() { return this.code; } private static final Map&lt;Integer,Status&gt; map; static { map = new HashMap&lt;Integer,Status&gt;(); for (Status v : Status.values()) { map.put(v.code, v); } } public static Status parse(int i) { return map.get(i); } } </code></pre> <p>So basically, the idea is to be able to get a <code>Status</code> by its <code>code</code>. And we keep some room between constants so that adding values is possible (it's not pretty but, well, it will work and should be safe for some time). </p> <p>And then in the entity:</p> <pre><code>@Entity public class Project { private Long id; private int statusCode; @Id @GeneratedValue public Long getId() { return this.id; } private void setId(Long id) { this.id = id; } @Transient public Status getStatus () { return Status.parse(this.statusCode); } public void setStatus(Status status) { this.statusCode = status.getCode(); } protected int getStatusCode() { return statusCode; } protected void setStatusCode(int statusCode) { this.statusCode = statusCode; } } </code></pre> <p>The getter/setter for the <code>int</code> representation are protected. The public getter/setter deal with the conversion.</p> <p>An alternative solution would be to use a custom type (at the cost of portability).</p> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/352586/how-to-use-enums-with-jpa">How to use enums with JPA</a> </li> <li><a href="https://stackoverflow.com/questions/2751733/map-enum-in-jpa-with-fixed-values">Map enum in JPA with fixed values ?</a> </li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload