Hi
I would like to check the used enums not only on the application layer
but also on the database. To ensure that all entries that are added
manually to the database are valid too. Therefor I've tried to use a
@ManyToOne mapping to ensure that hibernate creates the needed
foreign-key constraints.
Code:
@Entity
@Immutable
public enum Foo {
A("foobar", 1, 3L);
@Id
public final String value;
public final int value2;
@Transient
public final long value3;
private Foo(String value, int value2, long value3)
this.value = value;
this.value2 = value2;
this.value3 = value3;
}
}
@Entity
public class Bar {
@Id
private Long id;
@ManyToOne
@ForeignKey(name = "bar_foo_fkey")
private Foo foo;
}
Needless to say, that Hibernate is not able to retrieve Foo objects, as
an Exception (No default constructor for entity: Foo) occurs. Therefore
I've implemented a custom UserType that mapps from the String value to
an Foo enum. But I'm not able to able to combine @Type(type =
"FooUserType") with the @ManyToOne Attribute.
So at the Moment I've got the choice to use @Type or have Hibernate
generate foreign-key constraints via @ManyToOne. Who can I combine both?
thx
Mathias
Code: