开发者

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开发者_JS百科 within Android, to use these classes as standard POJOs?


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.


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.


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜