Using JPA, can we define an enum as id of an entity?
I’ve tried the following:
public enum AssetType {
....
}
@Entity
@IdClass(AssetType.class)
public class Adkeys {
private AssetType type;
@Id
@Enumerated(EnumType.STRING)
@Column(nullable = false)
public AssetType getType() {
return type;
}
}
Using OpenJPA, it complains:
org.apache.openjpa.persistence.ArgumentException: The id class “class aa.AssetType” specified by type “class aa.Adkeys” does not have a public no-args constructor.
So my questions are:
- should we able to use enum as id for an entity on JPA? (i.e. there is a bug in OpenJPA)
- or do I make a mistake somewhere?
- and is there any workaround for such problem?
The JPA spec doesn’t say this is possible:
If you really want to have a compile-time fixed number of records for a given entity, you can use a
Stringorintprimary key and assign itAssetType.FOO.name()orAssetType.FOO.ordinal()And non-portable here means that some persistence provider may support other things, but it might not work for another provider. As with the enum – if the persistence provider has special support for it, that does not try to instantiate it, but rather processes it specially after checking if
class.isEnum(), then it might work. But it seems your persistence provider doesn’t do this.