diek85
diek85

Reputation: 21

EJB3 persistence with a field that's not an entity

I've got the following situation

@Entity
public class myEntity implements Serializable {

    private AnotherClass anotherClass;
}


public class AnotherClass implements Serializable {

   private String ...
   private String ...


}

but AnotherClass IS NOT annotated with @Entity. I can successfully deploy my bean but when I try to persist a MyEntity class instance, it gives me NoSerializableException.

Is this because AnotherClass is not annotated with @Entity?

Upvotes: 2

Views: 158

Answers (1)

skaffman
skaffman

Reputation: 403581

Essentially, yes, although there's something else going on here.

Since AnotherClass is neither @Entity or @Embeddable, JPA may try to serialize it as a binary field instead. It seems to be trying this, but the NotSerializableException suggests that some other field of either MyEntity or AnotherClass isn't serializable, causing the exception.

You almost certainly don't want AnotherClass persisted as a binary, so you need to annotate it to tell JPA how to persist it.

Upvotes: 4

Related Questions