2

I'm passing JPA Entity called Profile from my server to client using GSON

@Entity  
public class Profile implements Serializable  
{
     private static final long serialVersionUID = 1L;
     @Id 
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;
     .
     .
     .
}

The GSON code:

Gson gson = new Gson();
Profile profile = gson.fromJson(json, Profile.class);

But I'm getting:

java.lang.NoClassDefFoundError: javax.persistence.Id
java.lang.reflect.Field.getDeclaredAnnotations(Native Method)
java.lang.reflect.Field.getDeclaredAnnotations(Field.java:188)
java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:188)
java.lang.reflect.AccessibleObject.getAnnotation(AccessibleObject.java:196)                               
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldName(ReflectiveTypeAdapt erFactory.java:60)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
com.google.gson.Gson.getAdapter(Gson.java:353)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
com.google.gson.Gson.getAdapter(Gson.java:353)
com.google.gson.Gson.fromJson(Gson.java:754)
com.google.gson.Gson.fromJson(Gson.java:721)
com.google.gson.Gson.fromJson(Gson.java:670)
com.google.gson.Gson.fromJson(Gson.java:642)

What I need to do to fix this?

Rami
  • 2,008
  • 5
  • 25
  • 37
  • 1
    Looks like a classpath issue to me. Is your serialization logic in a different archive from your entities? – Perception Apr 24 '12 at 17:58
  • Of course. I'm passing the Entity from client to server back and forth so I need to put it in someplace. Profile entity is in the server but i have claspasth to server from the client. – Rami Apr 24 '12 at 18:04
  • It looks very strange because unknown annotation should be silently ignored. Which JRE do you use? – axtavt Apr 24 '12 at 18:25

2 Answers2

1

IIRC, when you retrieve an object from EJB it is not of a class that you defined but of a stub class (which extends your class) created by the JPA. As GSON is using reflection (thereby bypassing the methods provided, which match your class specification-) it is retrieving the inner elements of the stub (which are not what you expect).

You can check it by doing a getClass() of the retrieved object.

As for how to solve it, you have two options:

a) Create your object by copying the JPA returned object.

b) Find a way to tell GSON to retrieve the data using the class methods and not reflection (I do not know if such way exists).

I don't know if there is some way to automatically retrieve the inner object from the JPA stub.

SJuan76
  • 23,682
  • 6
  • 41
  • 79
1

Add the geronimo-jpa_3.0_spec-1.1.1.jar (or the actual version you are using) to your android libs directory, and add it to the projects class path.

Guy
  • 11,597
  • 5
  • 47
  • 68
  • That may fix the problem but it shouldn't be neccessary to use any JPA libs on the client side. – andih May 17 '12 at 05:26