dasmaze
dasmaze

Reputation: 652

JPA Annotations in Android

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?

Upvotes: 14

Views: 6329

Answers (4)

John
John

Reputation: 373

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.

Upvotes: 2

P-A
P-A

Reputation: 1278

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>

Upvotes: 0

Patrick Roumanoff
Patrick Roumanoff

Reputation: 352

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.

Upvotes: 5

alphazero
alphazero

Reputation: 27224

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.

Upvotes: 8

Related Questions