Reputation: 21978
I found a forum with this subject. Using enum as id
But I couldn't sure that I can do it or not. I think my case is a little bit different.
enum Type {
}
class MyEntityId {
String type;
String key;
}
@Entity
@IdClass(MyEntityId.class)
class MyEntity {
@Enumerated(EnumType.String)
@Id
Type type;
@Id
String key;
}
Is this actually legal by the spec?
If it is, can I have no worries for vendor specific behaviors?
Upvotes: 3
Views: 2494
Reputation: 590
In Hibernate 4 you can use an Enum as an @Id
in an IdClass
, but only as an Ordinal. Any @Convert
tags you add the the enumeration will be ignored.
What you can do is use an @EmbeddedId
to get the same effect
@Embeddable
class MyEntityId {
@Enumerated(EnumType.String)
Type type;
String key;
}
@Entity
class MyEntity {
@EmbeddedId
MyEntityId id;
}
Upvotes: 3
Reputation: 15577
Why not read the linked issue ? It quotes the spec (2.1.4) and says NO you cannot have id fields of Enum type; it doesn't matter that you have some (invalid) IdClass (since the types of an IdClass have to match the types of the class). So it is not part of the JPA spec, so you are left with vendor-specific behaviour.
Upvotes: 4