0

ActiveJDBC is a real "thin" database mapper allowing to query databases and mapping the results to POJOs. I'm using version 2.1-SNAPSHOT.

However, when it comes to the properties of a class, it looks like one has to do the hard work manually (or I've overseen an essential piece).

Example: Let's assume there is a class Person with firstName and lastName:

public class Person {
    private String firstName;
    private String lastName;

    public String getFirstName() {
       return this.firstName;
    }

    public void setFirstName(String f) {
       this.firstName = f;
    }

    // same for lastName
}

Retrieving now the data is easy:

List<Person> persons = Person.findAll();

However it seems it's necessary to assign the properties manually to a class:

for(Person p : persons) {
    System.out.println(p.getLastName()) // ===> null
}

So the question is: Is it possible (and if so, how) to access a POJO's properties using usual getters and setters? Or is it possible to annotate the fields to map them accordingly to the database columns (e.g. first_name, last_name)?

I need to access some fields of the POJO quite often and I'd like to use the standard getters to do so. However, accessing the Model's get(property) method is quite expensive (not sure if Model.get() implies a SELECT statement?).

Now I ended up with something ugly like

String getFirstName() {
  if (this.firstName != null) 
     return this.firstName;

  Object o = get("first_name");
   if (o == null) 
      this.firstName = "";
   else 
      this.firstName = o.toString;

   return  this.firstName;
}

So... have I missed anything here?... Appreciate any help.

Georg S.
  • 23
  • 3
  • What does the documentation for the library say? What does your database schema look like? It's almost as if you did zero research before writing a question on SO. – Kayaman Feb 13 '18 at 07:46
  • I've indeed overseen a part of the documentation. SO is a platform to help each others. No need to get offensive. – Georg S. Feb 14 '18 at 15:42
  • SO is a platform with rules and guidelines. The ones who speak the most about "helping others" here are always the ones who are getting the free help. – Kayaman Feb 14 '18 at 15:49

2 Answers2

1

Sorry, but your code examples with properties do not correlate with the framework features and documentation. Also, a POJO is a Plain Old Java Object, which is not necessarily the same as a Java Bean, but you are conflating the two. For more information on ActiveJDBC attributes, refer to Getters and Setters.

Additionally, you can convert an ActiveJDBC model to a "bean", but you should think of them more like maps:

public class Person  extends Model{

    public String getFirstName() {
       return getString("first_name");
    }

    public void setFirstName(String f) {
       set("first_name", f)
    }
}
ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • Thanks for pointing out the difference - looks like you've just updated the documentation (section on getters/setters)? :) One should keep in mind that the above implementation of using get() / getString() _can_ be quite imperformant, depending on how often you're accessing the method. Could you also clarify if getString(propertyName) is Nullpointer-safe? – Georg S. Feb 13 '18 at 15:08
  • @GeorgS. this documentation is years old, not sure how you missed it. Performance of getters cannot be faster, just accessing a map. When writing your custom getters, you would hard-code the attribute name, so you will never have an NPE. Familiarize yourself with the docs. – ipolevoy Feb 13 '18 at 15:43
  • Yes I just must have overseen this section, my fault. I didn't get your last point regarding NPE: If the DB value of a column is NULL, what does getString() return then? That's what my last question was all about. Would be glad to find a hint on that in the JavaDocs (and here as well ;-)). Thanks! – Georg S. Feb 13 '18 at 21:33
  • @GeorgS., your question is about NPE, which is a NullPointerException, i.e. exception! If you have a NULL in the database, the framework will return `null` as a value and there will be no exceptions. Read: http://javalite.io/pass_through_framework – ipolevoy Feb 13 '18 at 21:40
  • 1
    thank you! Please don't get me wrong, there's always a balance between 'fast and easy' and 'comfortable'. Having a look on the design principle 'Should reduce amount of code to a minimum' and 'Should be very easy to work with', etc. then I see that it's cumbersome to access my POJO's fields using get(columnName). Even because you're already mapping table names to POJOs and use english inflection, I'm wondering why there are no annotations (or automatic features) to map columns to properties. At least I think it would be a great enhancement! Best regards Georg – Georg S. Feb 14 '18 at 08:10
  • Because these are not the properties. Models are not beans, they are maps. Writing a simple getter is not harder than annotation – ipolevoy Feb 14 '18 at 13:38
0

This is an old question, but it seems that people still need these setters/getters for the activejdbc objects. So I decided to create a simple annotation processor which will create them instead of me. As a bonus, you'll receive equals/hashcode, builder, and some more features which can make your life easier as well. If you are interested in it, you can find this annotation processor on maven central.

<dependency>
    <groupId>io.github.erioh</groupId>
    <artifactId>activejdbc-wrapper</artifactId>
    <version>1.0.3</version>
</dependency>

Documentation can be found here https://github.com/erioh/activejdbc-wrapper-project