zhangjingl02 / activejdbc

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

Exception when intermediate super class model is not backeup by table #155

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.
2.
3.

What is the expected output? What do you see instead?

Please use labels and text to provide additional information.

Original issue reported on code.google.com by ipolevoy@gmail.com on 24 Apr 2012 at 6:23

GoogleCodeExporter commented 9 years ago

public  class Party extends Model {
}

public class Person extends Party {
    static{
        validatePresenceOf("name", "last_name");
        convertDate("dob", "yyyy-MM-dd");
        convertTimestamp("graduation_date", "yyyy-MM-dd");
    }
}

@Test
    public void test() {
                Person person = new Person();
        person.validate();

    }

org.javalite.activejdbc.InitException: Failed to find table: parties
    at org.javalite.activejdbc.MetaModel.getAttributeNames(MetaModel.java:143)
    at org.javalite.activejdbc.MetaModel.checkAttributeOrAssociation(MetaModel.java:299)
    at org.javalite.activejdbc.Model.get(Model.java:1039)
    at org.javalite.activejdbc.test_models.Party.get(Party.java)
    at org.javalite.activejdbc.test_models.Person.get(Person.java)
    at org.javalite.activejdbc.validation.AttributePresenceValidator.validate(AttributePresenceValidator.java:36)
    at org.javalite.activejdbc.Model.validate(Model.java:1504)
    at org.javalite.activejdbc.test_models.Party.validate(Party.java)
    at org.javalite.activejdbc.test_models.Person.validate(Person.java)
    at org.javalite.activejdbc.HierarchyTest.test(HierarchyTest.java:67)

Original comment by ipolevoy@gmail.com on 24 Apr 2012 at 6:24

GoogleCodeExporter commented 9 years ago
This also fails on the set:

    @Test
    public void shouldGetName() {
        Person person = new Person();
        person.set("first_name", "fake first name", "last_name", "fake last name");

        String name = (String) person.get("first_name");
        the(name).shouldBeEqual("fake first name");
    }

Original comment by evan.leo...@gmail.com on 24 Apr 2012 at 6:37

GoogleCodeExporter commented 9 years ago
OK, fixing this defect would require major overhaul of instrumentation and 
refactoring of the Model class. The problem is that during runtime in Java it 
is incredibly hard to find out what class you are in from static methods. What 
is happening is that AJ looks at the stack trace and finds the first line with 
a class that is a Model and thinks that this is it. Unfortunately in this case, 
the first thing on the stack is Party, not Person. In order to fix this, we 
would have to completely remove all static method bodies from Model, only 
leaving empty methods, move  all code to a different class, then make changes 
to instrumentation to add this code back to models during instrumentation. I'd 
estimate this at about 40 hours of work. The upside from all this work will be 
MUCH! improved performance, eliminated castings from cases like this:

this:
Person p = (Person)Person.findById(x);
will be:
Person p = Person.findById(x);

So... what to do now? I'm sorry to say that for now I will have to defer this 
defect for the future and suggest to stay away from inheritance. Inheritance 
was contributed  by other members in the community, an apparently not very well 
tested. 
Eventually we will get to it though
So sorry, this is the first defect that was deferred :(
thanks
igor

Original comment by ipolevoy@gmail.com on 25 Apr 2012 at 5:55

GoogleCodeExporter commented 9 years ago
Okay. I think I can do what I need to with polymorphic associations.

Evan

On Apr 24, 2012, at 11:55 PM, activejdbc@googlecode.com wrote:

Original comment by evan.leo...@gmail.com on 25 Apr 2012 at 10:07