Using JPA generated Entity classes as class liberary in Android
Asked Answered
P

2

1

I am working on a client server application. in the Server side i have generated my entity data model with JPA2.0 and EclipseLink as provider. here is one of the classes

@Entity
@Table(name = "Customers")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Customers.findAll", query = "SELECT c FROM Customers c")})
public class Customers implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id 
    @Basic(optional = false)
    @Column(name = "SerialNoCST")
    private Integer serialNoCST;
    @Basic(optional = false)
    @Column(name = "SerialNOCSTSGLCST")
    private int serialNOCSTSGLCST;
    @Column(name = "NameGrpCST")
    private String nameGrpCST;
    @Column(name = "CodeCST")
    private String codeCST;
    @Column(name = "NameCST")
    private String nameCST;
    @Column(name = "AddressCST")
    private String addressCST;
    @Column(name = "TelCST")
    private String telCST;
    @Column(name = "DebtCredit")
    private Long debtCredit;
    @Column(name = "DocCredit")
    private Long docCredit;
    @Column(name = "CshCredit")
    private Long cshCredit;
    @Column(name = "DebtValue")
    private Long debtValue;
    @Column(name = "ClassNoCST")
    private String classNoCST;
    @Column(name = "PasCheqAmntCST")
    private Integer pasCheqAmntCST;
    @Column(name = "NPasBillValueCST")
    private Long nPasBillValueCST;
    @Column(name = "NPasBillAmntCST")
    private Integer nPasBillAmntCST;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Column(name = "MapXCst")
    private Float mapXCst;
    @Column(name = "MapYCst")
    private Float mapYCst;
    @Column(name = "MapImgCst")
    private Integer mapImgCst;
    @Basic(optional = false)
    @Column(name = "LastModified")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastModified;
    @Basic(optional = false)
    @Column(name = "IsDeleted")
    private boolean isDeleted;
    @Basic(optional = false)
    @Column(name = "RowID")
    private String rowID;
    @Basic(optional = false)
    @Column(name = "SerialNoDev")
    private int serialNoDev;

    public Customers() {
    }

    public Customers(Integer serialNoCST) {
        this.serialNoCST = serialNoCST;
    }

    public Customers(Integer serialNoCST, int serialNOCSTSGLCST, Date lastModified, boolean isDeleted, String rowID, int serialNoDev) {
        this.serialNoCST = serialNoCST;
        this.serialNOCSTSGLCST = serialNOCSTSGLCST;
        this.lastModified = lastModified;
        this.isDeleted = isDeleted;
        this.rowID = rowID;
        this.serialNoDev = serialNoDev;
    }

    public Integer getSerialNoCST() {
        return serialNoCST;
    }

    public void setSerialNoCST(Integer serialNoCST) {
        this.serialNoCST = serialNoCST;
    }

    public int getSerialNOCSTSGLCST() {
        return serialNOCSTSGLCST;
    }

    public void setSerialNOCSTSGLCST(int serialNOCSTSGLCST) {
        this.serialNOCSTSGLCST = serialNOCSTSGLCST;
    }

    public String getNameGrpCST() {
        return nameGrpCST;
    }

    public void setNameGrpCST(String nameGrpCST) {
        this.nameGrpCST = nameGrpCST;
    }

    public String getCodeCST() {
        return codeCST;
    }

    public void setCodeCST(String codeCST) {
        this.codeCST = codeCST;
    }

    public String getNameCST() {
        return nameCST;
    }

    public void setNameCST(String nameCST) {
        this.nameCST = nameCST;
    }

    public String getAddressCST() {
        return addressCST;
    }

    public void setAddressCST(String addressCST) {
        this.addressCST = addressCST;
    }

    public String getTelCST() {
        return telCST;
    }

    public void setTelCST(String telCST) {
        this.telCST = telCST;
    }

    public Long getDebtCredit() {
        return debtCredit;
    }

    public void setDebtCredit(Long debtCredit) {
        this.debtCredit = debtCredit;
    }

    public Long getDocCredit() {
        return docCredit;
    }

    public void setDocCredit(Long docCredit) {
        this.docCredit = docCredit;
    }

    public Long getCshCredit() {
        return cshCredit;
    }

    public void setCshCredit(Long cshCredit) {
        this.cshCredit = cshCredit;
    }

    public Long getDebtValue() {
        return debtValue;
    }

    public void setDebtValue(Long debtValue) {
        this.debtValue = debtValue;
    }

    public String getClassNoCST() {
        return classNoCST;
    }

    public void setClassNoCST(String classNoCST) {
        this.classNoCST = classNoCST;
    }

    public Integer getPasCheqAmntCST() {
        return pasCheqAmntCST;
    }

    public void setPasCheqAmntCST(Integer pasCheqAmntCST) {
        this.pasCheqAmntCST = pasCheqAmntCST;
    }

    public Long getNPasBillValueCST() {
        return nPasBillValueCST;
    }

    public void setNPasBillValueCST(Long nPasBillValueCST) {
        this.nPasBillValueCST = nPasBillValueCST;
    }

    public Integer getNPasBillAmntCST() {
        return nPasBillAmntCST;
    }

    public void setNPasBillAmntCST(Integer nPasBillAmntCST) {
        this.nPasBillAmntCST = nPasBillAmntCST;
    }

    public Float getMapXCst() {
        return mapXCst;
    }

    public void setMapXCst(Float mapXCst) {
        this.mapXCst = mapXCst;
    }

    public Float getMapYCst() {
        return mapYCst;
    }

    public void setMapYCst(Float mapYCst) {
        this.mapYCst = mapYCst;
    }

    public Integer getMapImgCst() {
        return mapImgCst;
    }

    public void setMapImgCst(Integer mapImgCst) {
        this.mapImgCst = mapImgCst;
    }

    public Date getLastModified() {
        return lastModified;
    }

    public void setLastModified(Date lastModified) {
        this.lastModified = lastModified;
    }

    public boolean getIsDeleted() {
        return isDeleted;
    }

    public void setIsDeleted(boolean isDeleted) {
        this.isDeleted = isDeleted;
    }

    public String getRowID() {
        return rowID;
    }

    public void setRowID(String rowID) {
        this.rowID = rowID;
    }

    public int getSerialNoDev() {
        return serialNoDev;
    }

    public void setSerialNoDev(int serialNoDev) {
        this.serialNoDev = serialNoDev;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (serialNoCST != null ? serialNoCST.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Customers)) {
            return false;
        }
        Customers other = (Customers) object;
        if ((this.serialNoCST == null && other.serialNoCST != null) || (this.serialNoCST != null && !this.serialNoCST.equals(other.serialNoCST))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.prs.model.Customers[ serialNoCST=" + serialNoCST + " ]";
    }

}

As a regular action i putted all my entity classes with my persistence.xml file in a jar file and added to another project which will provide crud operations of theses entity classes as Rest Methods.

After that i also added my model jar file to the android project, but in the runtime i got too many errors.

I want you to help me to know :

1- Is it possible to add my entity model jar file to android project? 2- I was going to write my own DAO layer with this jar file, is it have any conflict to use my entity classes and ContentProvider of Android API. 3- is there any problem with structure that i have chosen?

EDIT : Add The error stack trace

2012-12-13 11:23:28 - MobileAndroidSolution] Dx 
trouble processing "javax/xml/stream/EventFilter.class":

Ill-advised or mistaken usage of a core class (java.* or javax.*)
when not building a core library.

This is often due to inadvertently including a core library file
in your application's project, when using an IDE (such as
Eclipse). If you are sure you're not intentionally defining a
core class, then this is the most likely explanation of what's
going on.

However, you might actually be trying to define a class in a core
namespace, the source of which you may have taken, for example,
from a non-Android virtual machine project. This will most
assuredly not work. At a minimum, it jeopardizes the
compatibility of your app with future versions of the platform.
It is also often of questionable legality.

If you really intend to build a core library -- which is only
appropriate as part of creating a full virtual machine
distribution, as opposed to compiling an application -- then use
the "--core-library" option to suppress this error message.

If you go ahead and use "--core-library" but are in fact
building an application, then be forewarned that your application
will still fail to build or run, at some point. Please be
prepared for angry customers who find, for example, that your
application ceases to function once they upgrade their operating
system. You will be to blame for this problem.

If you are legitimately using some code that happens to be in a
core package, then the easiest safe alternative you have is to
repackage that code. That is, move the classes in question into
your own package namespace. This means that they will never be in
conflict with core system classes. JarJar is a tool that may help
you in this endeavor. If you find that you cannot do this, then
that is an indication that the path you are on will ultimately
lead to pain, suffering, grief, and lamentation.

[2012-12-13 11:23:28 - MobileAndroidSolution] Dx 1 error; aborting
[2012-12-13 11:23:28 - MobileAndroidSolution] Conversion to Dalvik format failed with error 1

thank you in advance.

Pluckless answered 13/12, 2012 at 7:38 Comment(4)
Please, Show us the trace errorScandal
Javier, i edit my post with error trace.Pluckless
See this: #4368355Scandal
i did what they have mentioned. no changes still got this errorPluckless
M
2

Android doesn't support JPA and none of the Java EE, etc. classes. You should make sure you classes only reference what is provided by the Android SDK.

And if you want to make sure that jar dependencies are linked into your app, you have to put them in the libs/ folder in recent version of Eclipse ADT.

Microscopy answered 13/12, 2012 at 8:12 Comment(4)
sorry i did not mentioned that i have include the persistence library in my project. i will include an image.Pluckless
It does in turn probably refer to classes that are not available.Microscopy
as I see with my understanding i have included everything. may be you are right about not adding the JPA entity classes to android project.Pluckless
See here for the classes and packages Android supports. "javax.xml.stream.EventFilter is not among them and probably other dependant classes as well. Generally, JEE stuf doesn't work on Android, you'll have to go for something more lightweight. developer.android.com/referenceMicroscopy
D
0

With android room you can create anotation based entities.

If you want to use the entities in android and in jpa you have to put both annotations to the entities.

Example: from App.java

package de.k3b.fdroid.domain.entity;

import static import androidx.room.ForeignKey;

import javax.persistence.Column;

/**
 * Information about an Android App.
 * <p>
 * Android independent: Pojo-s with all properties that are persisted in the Database.
 * Only primitives, primaryKeys and foreignKeys. No Relations or Objects or lists.
 * Database Entity compatible with Android-Room and non-android-j2se-jpa
 */
@androidx.room.Entity(foreignKeys = {@androidx.room.ForeignKey(entity = Repo.class,
        parentColumns = "id", childColumns = "resourceRepoId", onDelete = ForeignKey.SET_NULL)},
        indices = {@androidx.room.Index("id"), @androidx.room.Index({"packageName"})}
)
@javax.persistence.Entity
@javax.persistence.Table(name = "App")
@javax.persistence.Inheritance(strategy = javax.persistence.InheritanceType.SINGLE_TABLE)
public class App extends AppCommon implements AppDetail {
    public static final String NOT_FOUND_VALUE = "";

    @javax.persistence.Id
    @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
    @androidx.room.PrimaryKey(autoGenerate = true)
    private int id;

    @androidx.room.ColumnInfo(index = true)
    private Integer resourceRepoId;

Since the room annotations are only available for android apps in "aar-archive" you also need to make them available for non-android.

For more infos: I have documented a project with crossplatform entities here

Dakotadal answered 7/6, 2022 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.