zhangjingl02 / activejdbc

Automatically exported from code.google.com/p/activejdbc
0 stars 0 forks source link

Inheritance (abstract super class) + .parent throws an error "Failed to find table: abstract_my_models" #159

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create an abstract model "AbstractModel < Moddel"
2. Create two models that are backed by tables "MyFirstModel < AbstractModel" 
and "MySecondModel < AbstractModel"
3. Create a one to many relationship. And try to access via the method 
"parent".  e.g. MyFirstModel.findFirst("id = ?", 1).parent(MySecondModel.class).
4. An exception is generated.

NOTE: the many to many relationship works.  e.g. MyFirstModel.findFirst("id = 
?", 1).getAll(MyThirdModel.class).

What is the expected output? What do you see instead?
I expect to be able to load the "parent" in this case.

The error:
Exception in thread "main" org.javalite.activejdbc.InitException: Failed to 
find table: abstract_models
        at org.javalite.activejdbc.Model.parent(Model.java:862)
        at test.AbstractModel.parent(AbstractModel.java)
        at test.MyFirstModel.parent(MyFirstModel.java)
        ...
Caused by: org.javalite.activejdbc.InitException: Failed to find table: 
abstract_models
        at org.javalite.activejdbc.MetaModel.getAttributeNames(MetaModel.java:143)
        at org.javalite.activejdbc.MetaModel.getAttributeNamesSkipId(MetaModel.java:92)
        at org.javalite.activejdbc.Model.hydrate(Model.java:93)

What version of the product are you using? On what operating system?
1.2.3 latest trunk.  I also tried 1.2.2.

Please provide any additional information below.
http://code.google.com/p/activejdbc/wiki/Inheritance
indicates this should be possible.

I just started testing ActiveJDBC for possible use in a project.  At first 
glance it's not clear to my why an abstract class is treated as a "Model" as 
it's not possible to instantiate.  Also, there are a lot of warnings that 
tables do not exist.  My question is why not skip abstract classes via:

 !java.lang.reflect.Modifier.isAbstract( clazz.getModifiers() )

in ModelFinder#classFound and InstrumentationModelFinder#classFound.

This solved the problem with "parent" in this case.  Also, gets rid of the 
warnings about tables; as these classes are not meant to be models.  Let me 
know if there's more info I can provide.  Thanks!

Original issue reported on code.google.com by marc.har...@gmail.com on 4 May 2012 at 12:29

GoogleCodeExporter commented 9 years ago
Marc, unfortunately the inheritance implementation in ActiveJDBC was 
contributed from outside, and apparently not well tested. my fault for letting 
it out. Other users also noticed some irregularities with it. I'd suggest not 
to use it for now, but instead use composition - this will work.
I will put a warning on the wiki page

Sorry for trouble

Original comment by ipolevoy@gmail.com on 7 May 2012 at 6:39

GoogleCodeExporter commented 9 years ago
Hopefully, I can talk you into the attached patch.  I'm not really concerned 
about activejdbc fixing/supporting inheritance.  More that it not mess about 
with abstract classes and try to treat them as Models.  I don't think it would 
ever be valid to do something like:
  MyAbstractClass.findById(1)

I agree about composition.  This patch would just make my life simpler.  Thanks!

Original comment by marc.har...@gmail.com on 12 May 2012 at 5:52

Attachments:

GoogleCodeExporter commented 9 years ago
An other option is to create an annotation @NotModel or something similar.  

Original comment by marc.har...@gmail.com on 12 May 2012 at 6:21