We have a project that uses JPA/Hibernate on the server side, the mapped entity classes are in their own Library-Project and use Annotations to be mapped to the database. I want to use these classes in an Android-Project - is there any way to ignore the annotations within Android, to use these classes as standard POJOs?
4 Answers
The compiler will not care as long as you have the jar with the JPA annotation classes in your classpath. And if a runtime instrumentation system (such as Hibernate) is not present, the annotations are just extra info that are there but not used. The only issue is insuring the inclusion of the JPA jar in your distribution.

- 27,094
- 3
- 30
- 26
-
3And that might be a problem. I don't really want to bundle all the hibernate-JARs into the Android-Project. – dasmaze Oct 27 '09 at 14:36
-
If you can not bundle the required classes, then the only solution that I can think of is using a custom class loader that strips out the annotations from the bytecode. Its a non-trivial task but doable. – alphazero Oct 27 '09 at 15:06
-
p.s. The bytecode transformation doesn't necessarily has to happen on load. The other option is to run a process over the server libs and sanitize them for android by generating a new set of classfiles. – alphazero Oct 27 '09 at 15:07
The actual JPA annotations are part of Java SE 6, not hibernate. So you don't need to have the Hibernate JAR to be able to use JPA annotated classes. All you need is the classes/interfaces declaring the annotations your are referencing, if you can afford it the full JPA api jar is about 50k.

- 352
- 3
- 10
You can use Cmobilecom JPA for android and java. It implements JPA 2.1 specification. So all your JPA annotations for java will work for android without any changes. Simply add the following dependencies in your projects.
Android:
dependencies {
implementation("com.cmobilecom:cmobilecom-jpa-android:${version}@aar") {
transitive = true
}
annotationProcessor "com.cmobilecom:cmobilecom-jpa-processor:$version"
}
JDBC:
dependencies {
implementation "com.cmobilecom:cmobilecom-jpa-jdbc:$version"
// annotation processor: generate static metamodel
compileOnly "com.cmobilecom:cmobilecom-jpa-processor:$version"
}
Disclaimer: I am a developer of Cmobilecom JPA.

- 363
- 1
- 6
I just ran into this problem and since I use maven for building my android project I added this dependency
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>

- 1,161
- 12
- 16