ahmetb / orman

lightweight and minimalist ORM for Java/Android. works with SQLite & MySQL. (not actively maintained)
Other
249 stars 47 forks source link

org.orman.mapper.exception.FieldNotFoundException: Could not find property (field) #40

Closed fdutey closed 12 years ago

fdutey commented 12 years ago

Hi. I'm actually using orman on android and I think it's a really convenient orm.

Actually, I got a serious issue. I found a way to fix it temporary but It's not as clean as I would want. Let me explain:

I got 1 class named Noeud (node) and 1 classe named Equipement (equipment).

Noeud has two ManyToOne relationships. One with itself (on field "parent") and one to Equipement (on field "localisation").

My problem is: when I try to access equipment (by using node.getEquipements()), I got the following error: org.orman.mapper.exception.FieldNotFoundException: Could not find property (field) parent in Equipement.

I checked, double-checked, triple-checked my ManyToOne declarations (code below) but problem is still here). I have the impression that all ManyToOne associations of one class are sharing their foreign key names.

I solved the problem by renaming "localisation" field on Equipement to "parent" and it seems to work but it could be a problem if I was right about the cause.

class Noeud extends Model<Noeud> {
    @OneToMany(onField = Equipement.PROP_LOCALISATION, toType = Equipement.class, load = LoadingPolicy.LAZY)
    private EntityList<Noeud, Equipement> equipements = new EntityList<Noeud, Equipement>(Noeud.class, Equipement.class, this);

    @OneToMany(onField = "parent", toType = Noeud.class, load = LoadingPolicy.LAZY)
    private EntityList<Noeud, Noeud> children = new EntityList<Noeud, Noeud>(Noeud.class, Noeud.class, this);
}

class Equipement extends Model<Equipement> {
    public static final String PROP_LOCALISATION = "localisation"; //actually renamed to "parent"

    @ManyToOne(load = LoadingPolicy.LAZY)
    private Noeud localisation; //also renamed to "parent"
}

Do that sounds familiar to you? Did I miss something in documentation or in my code?

Best regards.

ahmetb commented 12 years ago

As far as I remember we are allowing more than one ManyToOne annotated fields in entities. There could be a bug in the framework, meanwhile could you please paste the complete exception stack?

It is quite interesting to get an exception on node.getEquipments() which is a getter probably you have created and only returns the EntityList instance. We may have limitations on usage of those cardinalities in one entity or maybe we didn't test EntityList<A, B> where A and B are actually the same classes. I'll investigate this. Sorry for the inconvenience.

fdutey commented 12 years ago

Here is my complete log

12-15 16:14:51.570: E/AndroidRuntime(16263): FATAL EXCEPTION: main
12-15 16:14:51.570: E/AndroidRuntime(16263): org.orman.mapper.exception.FieldNotFoundException: Could not find property (field) `parent` in Equipement.
12-15 16:14:51.570: E/AndroidRuntime(16263):    at org.orman.mapper.F.f(F.java:70)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at org.orman.mapper.Entity.getFieldByName(Entity.java:133)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at org.orman.mapper.EntityList.getTargetField(EntityList.java:363)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at org.orman.mapper.EntityList.refreshList(EntityList.java:115)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at org.orman.mapper.EntityList.lazyLoadIfNeeded(EntityList.java:77)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at org.orman.mapper.EntityList.add(EntityList.java:128)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at org.orman.mapper.EntityList.add(EntityList.java:38)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at com.imdeo.autocontroleMobile.helpers.NoeudHelper.localisationSerializeToNoeud(NoeudHelper.java:63)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at com.imdeo.autocontroleMobile.helpers.NoeudHelper.zoneSerializeToNoeud(NoeudHelper.java:52)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at com.imdeo.autocontroleMobile.helpers.NoeudHelper.niveauSerializeToNoeud(NoeudHelper.java:40)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at com.imdeo.autocontroleMobile.helpers.NoeudHelper.batimentSerializeToNoeud(NoeudHelper.java:28)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at com.imdeo.autocontroleMobile.helpers.ChantierHelper.chantierSerializedToChantier(ChantierHelper.java:27)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at com.imdeo.autocontroleMobile.helpers.ChantierHelper.loadChantierFromSI(ChantierHelper.java:75)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at com.imdeo.autocontroleMobile.helpers.ChantierHelper.synchronizeChantierDown(ChantierHelper.java:79)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at com.imdeo.autocontroleMobile.adapters.ListeChantierAdapter$OnSynchronizeButtonClickListener.onClick(ListeChantierAdapter.java:48)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at android.view.View.performClick(View.java:2408)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at android.view.View$PerformClick.run(View.java:8816)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at android.os.Handler.handleCallback(Handler.java:587)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at android.os.Looper.loop(Looper.java:123)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at android.app.ActivityThread.main(ActivityThread.java:4627)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at java.lang.reflect.Method.invokeNative(Native Method)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at java.lang.reflect.Method.invoke(Method.java:521)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-15 16:14:51.570: E/AndroidRuntime(16263):    at dalvik.system.NativeStart.main(Native Method)

There's no underlying exception (caused by).

All methods xxxSerializeToxxx (from helpers) are basic methods to convert objects from xml webservice to models (to solve some id problems).

I just put the code of one here, they're all the same:

private static Noeud localisationSerializeToNoeud(LocalisationSerialize localisation) {
    //LocalisationSerialize extends NoeudSerialize
    Noeud n = noeudSerializeToNoeud(localisation);

    n.insert();

    for(EquipementSerialize equipement : localisation.getEquipements()){
        n.getEquipements().add(EquipementHelper.equipementSerializeToEquipement(equipement));
    }

    return n;
}

I wasn't able to debug correctly in orman sources. For a reason that I don't understand, debugger is correctly highlighting orman code lines but is not able to display variable content.

If needed, here is my getter:

public List<Equipement> getEquipements() {
    return equipements; //EntityList<Noeud, Equipement>, as you can see in previous ticket.
}

Thanks for you help =)

tmarkle commented 12 years ago

It looks like you may have omitted the @Entity annotation in your classes.

ahmetb commented 12 years ago

Seems like true, it didn't throw any exceptions about @Entity annotation being not found since we're having issues with Dalvik VM (android) classloader. (discussed on #27). Thanks.

rudreshsp commented 9 years ago

hello sir need help asap this is wrt Foreign Key assume i have two tables Bank,State

1.Bank with fields id,bankname. 2.State with fields id,statename, bankid(this should map to id of Bank table) how to declare, and how to delete parent id, so that it should affect child row also so suppose if i delete a bank based on its id, it should be deleted in state also i mean bankid,

i have gone thru WIKi, but didnt understand , so explain with some complete example thanks in advance

Rudresh SP