Massimo Ugues
Massimo Ugues

Reputation: 4463

JPA generic field

Is it possible to persist a generic field?

I have this property on an Entity class

...
private T payload;
...

T extends EventMessagePayload

and

public interface StringPayload extends EventMessagePayload{ 
    String getPayload();
}

In my application i persist the field only when is of String type and during the save operation all works great.

When I read the object instead JPA try to create a String object but instead is a StringPaylod. Is there a way to intercept the creation and handle the object marshalling?

Upvotes: 4

Views: 2290

Answers (2)

Raymond
Raymond

Reputation: 957

We can. if the T implements Serializable

@Entity
public class IgsSubject extends BasicObject implements Serializable{

    private static final long serialVersionUID = -5387429446192609471L;

Upvotes: 0

Daniel
Daniel

Reputation: 28074

JPA itself does not allow this, but your JPA implementation might allow it. We once did this with Hibernate, and it boilds down to implement your own EntityTuplizer (and a HibernateInterceptor to map your objects back to HibernateEntities).

Upvotes: 5

Related Questions