Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORMLite many to many relation Android

I have two classes :

UniteStratigraphique.java :

    @DatabaseTable(tableName = "unitestratigraphique")
    public class UniteStratigraphique {
    public final static String ID_FIELD_NAME = "id";
        @DatabaseField(generatedId = true, columnName = ID_FIELD_NAME)
        private int id;
    // CAMPAGNES
        @DatabaseField(foreign = true, foreignAutoRefresh = true)
        private Campagne campagne;
        @ForeignCollectionField
        private ForeignCollection<Campagne> listeCampagnes;

public UniteStratigraphique() {}

    public Campagne getCampagne() {
            return campagne;
        }

        public void setCampagne(Campagne campagne) {
            this.campagne = campagne;
        }

        public ArrayList<Campagne> getListeCampagnes() {
            ArrayList<Campagne> campagnesArray = new ArrayList<Campagne>();
            for (Campagne campagne : listeCampagnes) {
                campagnesArray.add(campagne);
            }
            return campagnesArray;
        }

        public ForeignCollection<Campagne> getListeCampagnesForeign() {
            return listeCampagnes;
        }

        public void setListeCampagnes(ForeignCollection<Campagne> listeCampagnes) {
            this.listeCampagnes = listeCampagnes;
        }

    }

Campagne.java :

@DatabaseTable(tableName = "campagne")
public class Campagne {
@DatabaseField(generatedId = true)
    private int id;
// UNITE STRATIGRAPHIQUE
    @ForeignCollectionField
    private ForeignCollection<UniteStratigraphique> listeUniteStratigraphique;
    @DatabaseField(foreign = true, foreignAutoRefresh = true)
    private UniteStratigraphique uniteStratigraphique;
public Campagne() {}

public ArrayList<UniteStratigraphique> getListeUniteStratigraphique() {
        ArrayList<UniteStratigraphique> usArray = new ArrayList<UniteStratigraphique>();
        for (UniteStratigraphique us : listeUniteStratigraphique){
            usArray.add(us);
        }
        return usArray;
    }

    public ForeignCollection<UniteStratigraphique> getListeUniteStratigraphiqueForeign() {
        return listeUniteStratigraphique;
    }

    public void setListeUniteStratigraphique(
            ForeignCollection<UniteStratigraphique> listeUniteStratigraphique) {
        this.listeUniteStratigraphique = listeUniteStratigraphique;
    }

    public int getSizeListeUniteStratigraphique() {
        return listeUniteStratigraphique.size();
    }

    public UniteStratigraphique getUniteStratigraphique() {
        return uniteStratigraphique;
    }

    public void setUniteStratigraphique(UniteStratigraphique uniteStratigraphique) {
        this.uniteStratigraphique = uniteStratigraphique;
    }
}

As you can see, these are Many-To-Many linked (0...n---0...n, with ORMLite annotations).

Now, my workflow is :


I create multiple "UniteStratigraphique" classes and I store them into my database (this works fine).

=> So I have n * "UniteStratigraphique" stored.

After that what I want is to create a "Campagne" class wich will contain multiple "UniteStratigraphique" classes.

=> So I want to set this field from "Campagne.java" :

 @ForeignCollectionField
    private ForeignCollection<UniteStratigraphique> listeUniteStratigraphique;

with the n * "UniteStratigraphique" elements I just stored before.

I tried to do this with this DAO method but it only duplicate the "UniteStratigraphique" classes into my db and no link is made..

 public void addUsToCampagne(Campagne campagne,
            ArrayList<UniteStratigraphique> usArray) {
        ForeignCollection<UniteStratigraphique> usForeign = campagne
                .getListeUniteStratigraphiqueForeign();
        if (usForeign == null) {
            try {
                usForeign = getHelper().getCampagneDao()
                        .getEmptyForeignCollection("listeUniteStratigraphique");

                for (UniteStratigraphique us : usArray) {
                        usForeign.add(us);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else{
            for (UniteStratigraphique us : usArray) {
                    usForeign.add(us);
            }
        }

    }

And in my Activity I'm doing this :

db.addCampagne(campagne);

            if( myUniteStratigraphiqueArray.size() > 0){
                    db.addUsToCampagne(campagne, myUniteStratigraphiqueArray);
            }
like image 974
eento Avatar asked Aug 19 '13 08:08

eento


1 Answers

Many to Many relations are non automatic with ORMLite, the only way to achieve it is to make a 3rd Table only for link beetween these 2 classes..

This link refers to this problem : What is the best way to implement many-to-many relationships using ORMLite?

And the example here : https://github.com/j256/ormlite-jdbc/tree/master/src/test/java/com/j256/ormlite/examples/manytomany

Hope it helped.

like image 158
eento Avatar answered Sep 29 '22 10:09

eento