Using POJOs (Plain Old Java Objects)

There are a few requirements for your Java POJOs:
  1. Implement java.io.Serializable.
  2. Have a public zero-argument constructor.
  3. Follow the JavaBeans naming conventions for getters and setters.
  4. Not have logic in your JavaBeans.
Here's an example POJO:
public class Bar implements Serializable {      
        private String name;
        private Integer id;
        private List bazs;
        
        public List getBazs() {
                return bazs;
        }
        public void setBazs(List bazs) {
                this.bazs = bazs;
        }
        public Integer getId() {
                return id;
        }
        public void setId(Integer id) {
                this.id = id;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
}
Note that because the Bar class has no constructors, the compiler automatically creates a zero-argument constructor. If your class has a constructor that takes one or more arguments, you must write a zero-argument constructor.

Your POJOs can contain any normal kinds of data:

  • Strings
  • Primitives like int, long, double, float, etc.
  • Primitive wrappers like Integer, Long, Double, Float, etc.
  • Dates (java.util.Date, java.sql.Date, java.sql.Timestamp are all treated the same).
  • References to other POJOs
  • Arrays of any of those types
  • Inheritance of other POJOs

Your POJOs should not contain:

  • Business logic. The JavaScript classes made by the EntityGenerator will not contain any business logic or other supporting logic, unless you code it yourself.
  • Inner classes. Not supported at this time.
  • Persistence logic. Again, this kind of information won't be represented on the JavaScript versions of your classes.
Once you've imported your POJO entity classes using <jsorb:import>, using them is very simple. You can instantiate an object of class com.foo.Bar with this code:
var bar = new com.foo.Bar();
Your objects will have all the properties of their Java counterparts, accessible through the normal getter/setter functions:
var value = bar.getSomeValue();
bar.setSomeValue(value + 1);

Primitives

One area where Java and Javascript are distinctly different is in their handling of primitives. Java 5 changes this a bit with its autoboxing functionality, but there are other notable differences even then. JavaScript variables are untyped. It's difficult and sometimes impossible to tell the difference between integers, strings, characters, etc. in JavaScript code. Most of the time JsOrb handles casting data to the correct type transparently, but there are a few situations (mostly dealing with untyped references) where it will need some help determining the correct data types to use when unmarshaling your data in your application.
  • Characters. JavaScript has no Character type.
  • Numbers. JavaScript doesn't distinguish between different numeric types: all numbers are treated similarly to Java Doubles in that they are nullable, unranged, and floating point with limited precision.
  • Arrays. JavaScript has arrays, but they are untyped (similar to Java's Object[]). Also arrays in JavaScript can be associative, meaning they have non-integer indexes (e.g., foo["Fred"]).

Collections

JsOrb includes partial implementations of the Java Set, List, and Map interfaces:
var list = new js.util.List();
list.add(bar);

Arrays

Arrays are fully supported as native JavaScript objects. [TBD]