2

If understood correctly, ActiveJdbc compiler should take as input hollow class like this

public class Employee extends Model {}

and fill it with some code from database metadata.

But how can it know where database is located?

I found only one place here http://javalite.io/getting_started where database is mentionned, namely

Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/test", "user1", "xxxxx");

So, does instrumentation compiler scans code for calls to Base.open() and parses it for URL?

I can't believe it. What if there are multiple calls to different databases?

What if there is no Base.open() call?

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • Have you read http://javalite.io/instrumentation ? I don't think it connects to the database to do its job, which is mainly creating the static methods. It looks like it uses only what is described it the class code. – ericbn Sep 08 '14 at 13:12
  • Nothing is described in class code. Classes are hollow in `ActiveJdbc`. Is is said in your link "Without instrumentation, ActiveJDBC would not be able to know what table to query." This means, that WITH instrumentation ActiveJDBC does know. My question is how? – Suzan Cioc Sep 08 '14 at 13:15

1 Answers1

2

Looking at the activejdbc-instrumentation source, what is does basically is:

  • Find non-abstract subclasses of org.javalite.activejdbc.Model and for each class
    • Add methods that delegate to org.javalite.activejdbc.Model, which include:
      • public static MetaModel getMetaModel()
      • public static List<String> attributes()
      • public static List<Association> associations()
      • public static int delete(String query, Object... params)
      • public static boolean exists(Object id)
      • public static int deleteAll()
      • public static int update(String updates, String conditions, Object ... params)
      • public static int updateAll(String updates, Object ... params)
      • and more ...
    • Add public static String getClassName() method that returns the fully-qualified name of the class.
    • Add a line to the activejdbc_models.properties file containing model.getName() + ":" + getDatabaseName(model) + "\n", where the first method returns the fully-qualified name of the class, and the second method returns the value of the @DbName annotation on the class or "default" if no annotation is found.

All database metadata is resolved in runtime, not during compilation or instrumentation phase.

ericbn
  • 10,163
  • 3
  • 47
  • 55
  • 1
    This is a correct answer. ActiveJDBC, like ActiveRecord reads metadata from the database the first time you use any model class. Add `activejdbc.log` system property to see the output as it discovers tables and relationships. For more on that, refer to: http://javalite.io/logging – ipolevoy Sep 09 '14 at 05:47
  • @ipolevoy, can you please explain why this delegation is needed? – ericbn Sep 12 '14 at 22:58
  • I assume you are asking why instrumentation is needed. If this is the case, see this page: http://javalite.io/instrumentation – ipolevoy Sep 13 '14 at 22:01