6

I have an entity that contains an enum property. That property can be any one of my types of Enums in my codebase:

public enum AutomobileType {
   CAR, TRUCK, MOTORCYCLE
}

public enum BoatType {
   ROW_BOAT,YACHT,SHIP
}

@Entity
public class FooBar {

  @Enumerated(value=EnumType.ORDINAL)
  private Enum enumValue;

  public void setEnumValue(Enum value) { ... }
  public Enum getEnumValue() { ... }
}

This fails with an exception, "Wrong data type: For input string: "[B@f0569a". I changed FooBar to store the property as an Integer which works, but that's not what I need. I need the actual enum. Any suggestions on how to make this work so that the Enum can be persisted as int but later pulled out into the correct Enum type?

1
  • "Any suggestions on how to make this work so that the Enum can be persisted as int but later pulled out into the correct Enum type?" - You mean, you want to store "1" and then get it back as AutomobileType? Commented Jan 14, 2011 at 14:04

3 Answers 3

3

You need to define a custom UserType for the Enum.

Sign up to request clarification or add additional context in comments.

3 Comments

That doesn't apply to JPA, surely, only for XML-configured Hibernate.
I think it's the only way for polymorphic enums, although the referenced code sample should be modified accordingly.
It is somewhat ridiculous that JPA doesn't have a standard for doing custom column/property bindings. You have to do it manually in the entity by having two sets of properties and marking one set as transient.
2

Generally, you should not do that. If the two (or more enums) can be assigned to the same field, then merge them into one anum.

If you are using inheritance, like: Vehicle with two subclasses - Boat and Car, then you can have a different field in each subclass - after all each of these enums is relevant only to the particular type. So:

@Column
private BoatType boatType;

3 Comments

@Bozho: He wants to store different enums in the same property.
I think the OP means that he actually wants to use a polymorphic Enum, rather than a specific enum type.
If I understand the question, codecraig is not trying to persist a specific Enum, but any.
0

I don't think this can work easily with multiple Enum types trying to co-exist in the same property. How does hibernate know which Enum class it should be instantiating when it loads the field out of the DB?

If you really want to do this you'd need to somehow encode the enum class + the value every time you persist this property (use a custom UserType as duffymo suggests).

Can you not break this into two proprties, one per enum class? Then you can declare enumValue (and enumValue2) as the correct class and it will work out of the box.

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.