i want to use an enum in an entity bean. But the enum is in an embedded object. There is the code:
@Entity
@Table(name = "entity_foo")
public class EntityFoo implements Serializable
{
@Embedded
private EmbeddedFoo embeddedFoo;
public EmbeddedFoo getEmbeddedFoo()
{
return embeddedFoo;
}
public void setEmbeddedFoo(final EmbeddedFoo embeddedFoo)
{
this.embeddedFoo = embeddedFoo;
}
}
My Embedded Object includes the enum and looks like this:
@Embeddable
public class EmbeddedFoo implements Serializable
{
public static enum EnumBar {
VALUEA,VALUEB
}
private EnumBar enumBar;
@Enumerated(EnumType.STRING)
public EnumBar getEnumBar()
{
return enumBar;
}
public void setEnumBar(final EnumBar enumBar)
{
this.enumBar = enumBar;
}
}
In the table entity_foo i declared the value enumbar as varchar(255). Now i try to get data from the database.
final List<EntityFoo> entityFoos = query.getResultList();
This throws an PSQLException:
Caused by: org.postgresql.util.PSQLException: Bad value for type int : VALUEA
If i use the enum directly in the entity "EntityFoo" it works fine. This code runs on Wildfly 8.1 with Postgres 9.3 and Java 1.7
I hope i declared my problem clearly and anyone can help me.
UPDATE
Here is the link to a miniproject repository https://github.com/MotherCake/miniprojct
To use this project you need a table. Here is the create statement.
CREATE TABLE "public"."minitable"(id int PRIMARY KEY NOT NULL,enumbar varchar(255));
CREATE UNIQUE INDEX minitable_pkey ON "public"."minitable"(id);
INSERT INTO minitable (id,enumbar) VALUES (0,'VALUEA');
INSERT INTO minitable (id,enumbar) VALUES (1,'VALUEB');
What i forgot to mention is that i'm using Toplink. I think Toplink causes the problem.
In the class Testservlet you can see 2 different ways to create a query. The exception i described is thrown if i use the createQuery(..) method with the setParameter(..) method. This is in the comment.
If i'm using the method createNativeQuery i get the result list and no exception is thrown. But after that i get a ClassCastException in class TestServlet.