There are many ways to create new records with ActiveJDBC. Let's explore them
Use setter method
1 2 3 4 5
This code should be self explanatory. As you can see, ActiveJDBC does not require to have getters and setters. You can write them, if you like, but IMHO, they are nothing but code pollution.
save() and saveIt() methods
ActiveJDBC class Model provides two methods for saving an entity:
saveIt(). Both methods will involve validations during saving, but in the case of the method save() will silently exit without throwing exceptions. In case validations failed, the instance will have an errors collection attached to it. This is very useful in the context of a web application. Here is an example:
1 2 3 4 5 6 7 8
More on validations , see this page: Validations
saveIt() method will throw an exception in case there was a validation problem. The
save() method makes more sense in the context of a web application, whereas
saveIt() is more useful in a non-web app situations - batch inserts, tests, etc.
set(name, value) method returns reference to the same model object, which makes it possible to string method calls like this:
..or make it shorter:
Batching names an values
There is a way to batch names and values into arrays:
1 2 3
Of course, the names is a String array and the two arrays need to be the same size.
Initialize from Map
This method of creation is useful for web applications if request parameters are posted from a form an available in a Map instance:
1 2 3 4
Initialize from varargs
Model also provides another convenience method for entity initialization, the set methods that accepts a varargs:
1 2 3
The argument list is a string of names and corresponding values where names are interleaved with values. This makes it easy to write allows for easy reading (if you just read it aloud, it will sound as an English sentence).
Convenience create() and createIt() methods
The class Model also provides two convenience methods for creation of records:
createIt(). There is a semantic difference between these two methods, and it is the same as between
saveIt() methods, except in this case, ActiveJDBC creates and attempts to save an object in one step.
createIt() method accepts a list of arguments, where names are interleaved with values. This is similar to the varargs setter described above, but also includes semantics of the
Please, see Setters and getters
How to comment
The comment section below is to discuss documentation on this page.
If you have an issue, or discover bug, please follow instructions on the Support page