Tables.

Tables are the base object that then generates the model objects for interaction through the java code. As such they are relatively straight-forward, the field definitions are the next important piece of the h2zero generation.

For each database schema, there must be a JSONArray keyed on tables of JSONObjects

"tables": [
	{
		"name": "user_type",
		"engine": "innodb",
		"charset": "UTF8",
		"comments": [			"comment for the table, which is an array and can be on multiple",			"lines"
		],
		...
	},
	{
		"name": "user",
		...
	},
]
JSON keyusageallowable valuesnotes
namemandatoryany valid MySQL table name valueEach table MUST have a name. It is recommended to use snake_case for field naming.
engineoptionalAny valid MySQL storage engine type, although it is recommended to useInnodb. The available storage types are:MyISAM, InnoDB, Memory, Merge, Archive, Federated, NDB, CSV, BlackholeThe default is innodb
charsetoptionalAny valid MySQL character setBy default this is set to UTF8.
commentsoptionalA JSON Array of stringsThese do not affect the code generation, but they do insert comments where possible

Each table MUST have a key of name which will map to the database schema and model object in CamelCase naming conventions.

For example the user_type table name above will generate the following model object

The user_type table is a Constant Model and differs from normal other models in that constant tables cannot be updated, deleted or inserted into as they are pre-generated by h2zero.

The user table name above will generate the following model object

The user table is a Regular Model object that maps to the underlying MySQL table and h2zero will allow generation of all create, read, update and delete (CRUD) methods.

Models in General

Models come in two flavours, regular and constant. They both share some base features and functionality asdetailed below:

BINDER

// the binder is unused in code, but will generate compile problems if this 
// class is no longer referenced in the h2zero file. Just a nicety for
// removing dead code
@SuppressWarnings("unused")
private static final String BINDER = Constants.USER_TYPE_binder;

As mentioned in the comments above the field, the BINDER is only used as a compile time error so that dead code can be removed. Most of the generated code contains this code that references the constant. This will ensure that when model names are changed, or any of tyhe interaction names are changed (e.g. finders, questions, counters etc.)

PRIMARY_KEY_FIELD

public static final String PRIMARY_KEY_FIELD = "id_user_type";

This is a reference to the primary key field in the database. Every table (and therefore every model) must have a primary key on it.

Member variable declarations

Each field that is attached to the table will generate a declaration.

private Long idUserType = null;
private String nmUserType = null;

Constructor

Every model has a constructor with a signature that contains all of the fields. Despite the fact that you cannot create new constants, the constant models still include a constructor for use with finders, counters and questions.

public UserType(Long idUserType, String nmUserType) {
	this.idUserType = idUserType;
	this.nmUserType = nmUserType;
}

Getters

/*
 * Boring ol' getters and setters 
 */

public Long getIdUserType() {return(this.idUserType);}
public String getNmUserType() {return(this.nmUserType);}

Getters are generated (in condensed format) for each field on the table. No setters are generated for constant models as you may not change constants.

String methods

public String toString() {
	StringBuilder stringBuilder = new StringBuilder();
	stringBuilder.append("Model[UserType]\n");
	stringBuilder.append("  Field[idUserType:" + this.idUserType + "]\n");
	stringBuilder.append("  Field[nmUserType:" + this.nmUserType + "]\n");
	return(stringBuilder.toString());
}

public String toJsonString() {
	// !!! work in progress !!!
	StringBuilder stringBuilder = new StringBuilder();
	stringBuilder.append("{\n");
	stringBuilder.append("  \"model\": {\n");
	stringBuilder.append("    \"name\": \"UserType\",\n");
	stringBuilder.append("    \"fields\": [\n");
	stringBuilder.append("     { \"name\": \"idUserType\", \"value\": " + this.idUserType + " }, \n");
	stringBuilder.append("     { \"name\": \"nmUserType\", \"value\": \"" + this.nmUserType + "\" }\n");
	stringBuilder.append("    ]\n");
	stringBuilder.append("  }\n");
	stringBuilder.append("}\n");
	return(stringBuilder.toString());
}

public String getJsonString() {
	return(toJsonString());
}

The string methods are automatically generated for each model. As the comment notes above, it is a work in progress. Care must be taken when utilising this method on sensitive information contained within the model, e.g. passwords, credit card details etc.

The getJsonString() method signature above is for JSTL or templar referencing for output.

Constant Models

See the section on constants for more information on how to generate them.

Regular Models

Regular models are the default generation for h2zero and, as opposed to constants, allow access to insert new rows into the database and have access to the full range of interactions. This is the foundation for the h2zero generation.

Additional methods and member variables

In addition to the general generation mentioned above, for each of the constant models, the following field and method signatures are generated:

Static Final Member Variable References

private static final String SQL_INSERT = "insert into user values (?, ?, ?, ?, ?, ?, ?, ?)";
private static final String SQL_UPDATE = "update user set id_user_type = ?, fl_is_alive = ?, num_age = ?, nm_username = ?, txt_address_email = ?, txt_password = ?, dtm_signup = ? where " + PRIMARY_KEY_FIELD + " = ?";
private static final String SQL_DELETE = "delete from user where " + PRIMARY_KEY_FIELD + " = ?";
private static final String SQL_ENSURE = "select " + PRIMARY_KEY_FIELD + " from user where id_user_type = ? and fl_is_alive = ? and num_age = ? and nm_username = ? and txt_address_email = ? and txt_password = ? and dtm_signup = ?";

The basic CRUD operations for the model. Each model knows how to create, read, update and delete itself from the database. There is also an 'ensure' mode, which will be covered later in this documentation set.

Constructors

public User(Long idUser, Long idUserType, Boolean flIsAlive, Integer numAge, String nmUsername, String txtAddressEmail, String txtPassword, Timestamp dtmSignup)
public User(Long idUser, Long idUserType, Integer numAge, String nmUsername, String txtAddressEmail, String txtPassword) 

Along with the defaut constructor with parameters for all fields, there is also a constructor for all of the non-null fields within the table.

Interactions

Please see the method signatures page for information about usage patterns for the various method signatures that are generated.

inserts - CRUD operators (and then some)

public void insert(Connection connection) throws SQLException, H2ZeroPrimaryKeyException 
public void insertSilent(Connection connection) 
public void insert() throws SQLException, H2ZeroPrimaryKeyException 
public void insertSilent() 

When a new java model is instantiated, this will insert it into the database and retrieve the primary key from the insertion and populate the member variable.

ensure - CRUD operators (and then some)

public void ensure(Connection connection) throws SQLException, H2ZeroPrimaryKeyException 
public void ensureSilent(Connection connection) 
public void ensure() throws SQLException, H2ZeroPrimaryKeyException 
public void ensureSilent() 

Ensure that there is a row in the database table with exactly the matching member variables. If it exists, the primary key variable will be set, otherwise it will be inserted. I.e. Ensure that there is a row in the database.

updates - CRUD operators (and then some)

public void update(Connection connection) throws SQLException, H2ZeroPrimaryKeyException 
public void updateSilent(Connection connection) 
public void update() throws SQLException, H2ZeroPrimaryKeyException 
public void updateSilent() 

Persist the model to the database, only if there have been some changes to the model object (i.e. has the flag isDirty sit), otherwise it will do nothing.nothing.

Note: the method signatures that throw an H2ZeroPrimaryKeyException do so if the primary key is null.

insert or updates - CRUD operators (and then some)

public void insertOrUpdateSilent() 

Insert the model if the primary key is null, otherwise update it (subject to the same caveats as an insert above).

deletes - CRUD operators (and then some)

public void delete(Connection connection) throws SQLException, H2ZeroPrimaryKeyException
public void deleteSilent(Connection connection) 
public void delete() throws SQLException, H2ZeroPrimaryKeyException 
public void deleteSilent() 

Delete this model from its corresponding row in the database.

Note: the method signatures that throw an H2ZeroPrimaryKeyException do so if the primary key is null.

refreshes - CRUD operators (and then some)

public void refresh() throws H2ZeroPrimaryKeyException 
public void refreshSilent() 

Foreign key references

public UserType getUserType() 

Where there is a foreign key reference to the table, not only can you retrieve the id for the foreign key, you may also get a direct reference to the model object, which will automatically find the object in the foreign table.

In the above code example this will retrieve the UserType object from the database with the associated primary key.

Note: In the case of a constant model (as is above) the lookup field on the constant model will be used, rather than a database query.

Getters and setters

public void setIdUser(Long idUser) {if(isDifferent(this.idUser, idUser)) {this.idUser = idUser;this.isDirty = true;}}

All getters are generated, the setters are generated (in highly compressed format) with a conditional check to ensure that if the member variable has not changed, the model will not be marked as dirty and any call to the update method signatures will do nothing.