^ Inspired By
GWT & Hibernate http://www.gwtproject.org/articles/using_gwt_with_hibernate.html
Code generator vs ORM http://stackoverflow.com/questions/4788248/code-generator-vs-orm
What’s everyone’s take on ORM or plain SQL? https://www.reddit.com/r/golang/comments/ircaxk/whats_everyones_take_on_orm_or_plain_sql/
What is a Data Transfer Object (DTO)? https://enroute.osgi.org/FAQ/420-dtos.html
Why are Data Transfer Objects an anti-pattern? http://stackoverflow.com/questions/1440952/why-are-data-transfer-objects-an-anti-pattern
Entity To DTO Conversion for a Spring REST API https://www.baeldung.com/entity-to-and-from-dto-for-a-java-spring-application
Data Access Object Pattern https://www.tutorialspoint.com/design_pattern/data_access_object_pattern.htm
How to design a DAO class? http://stackoverflow.com/questions/7232080/how-to-design-a-dao-class
One Big DAO, or One DAO Per Table/Object? https://dzone.com/articles/one-big-dao-or-one-dao
On the proper usage of DAOs http://stackoverflow.com/questions/9000867/on-the-proper-usage-of-daos
Data access object (DAO) in Java https://stackoverflow.com/questions/19154202/data-access-object-dao-in-java
The DAO Anti-patterns http://rrees.me/2009/07/11/the-dao-anti-patterns/
Data access with JDBC http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html
Is there a Eclipse plugin for Spring JDBCTemplate code generation? http://stackoverflow.com/questions/6007260/is-there-a-eclipse-plugin-for-spring-jdbctemplate-code-generation
is there any DAO or DTO generators for mysql in python ? http://stackoverflow.com/questions/3976480/data-access-object-in-python
^ CRUD code for all data tables/views in 5 minutes

1. Generate declarations of DTO classes (Plug-in GUI -> tab 'DTO' -> local tool-bar -> DTO CRUD XML Assistant):

<dto-class name="Project" ref="projects" />
<dto-class name="Task" ref="tasks" />

Copy-paste generated XML tags to 'dto.xml'.

Generate DTO classes.

2. Generate XML declarations of CRUD actions (Plug-in GUI -> tab 'DAO' -> local tool-bar -> DAO CRUD XML Assistant):

<crud-auto dto="Project" table="projects" />
<crud-auto dto="Task" table="tasks" />
<query-dto-list method="get_tasks_by_project(p_id)" dto="Product" ref="tasks(p_id)" />

Copy-paste generated XML tags to existing DAO XML files. Use Plug-in GUI -> tab 'DAO' -> local tool-bar -> 'New XML File' if needed.

Generate DAO classes. DONE.

^ If you want to create "type-map" from zero

Start with this:

<type-map default="">
</type-map>

Code generator will render something like this:

type Actor struct {
    ActorId java.lang.Integer // not a Go type!
    ...

Extend type-map:

<type-map default="">
    <type detected="java.lang.Integer" target="int64" />
</type-map>
type Actor struct {
    ActorId int64
    ...

Do the same for all detected types.

Only for Go so far: if you need additional imports, use this

<type detected="java.util.Date" target="time:time.Time"/>
<type detected="gorm-java.util.UUID" target="github.com/google/uuid:uuid.UUID${json-gorm}"/>
^ Android + Java + SQLite3
  • SQLite3 database file must be located on both Android-Device and Development-PC. A development/testing version of this file is also OK: just ensure identical Schemas.
  • To generate the code, use JDBC driver like this: xerial / sqlite-jdbc.
  • An example of DataStoreManager.java for Android is shipped with the plug-ins (tab 'Admin'). Also, it is available on GitHub.
  • JDBC drivers for SQLite3 do not provide information about the types of ResultSet columns and SQL parameters. If you need to generate strongly-typed code, specify the types in XML:
    <dto-class name="Board" ref="get_boards.sql" >
        <field column="b_id" type="Long" />
        <field column="b_name" type="String" />
        <field column="b_dt" type="String" />
        <field column="b_tasks_count" type="Long" />
    </dto-class>
  • Problem:
    1. In SQLite3, only INTEGER-type is allowed for AUTOINCREMENT PRIMARY KEY.
    2. In CRUD scenario, INTEGER is detected by code generator (SQLite3 JDBC driver) as java.lang.Integer.
    3. On Android-side (at run-time), INTEGER-s are fetched as java.lang.Long and exceptions are thrown.

    Workaround in 'settings.xml like this:

    <type-map default="">
        <type detected="java.lang.Integer" target="java.lang.Long"/>
    </type-map>

    or this:

    <type-map default="">
        <type detected="java.lang.Integer" target="Long --> SetLong"/>
    </type-map>
^ Strong typing in generated DAO classes

NOTE: only for Go and Java so far.

This is how it looks alike:

<type-map default="">
    <type detected="java.lang.Integer" target="int64${json} -> SetInt64"/>
    <type detected="java.lang.Double" target="float64${json} -> SetFloat64"/>
    <type detected="java.lang.String" target="string${json} -> SetString"/>
    <type detected="byte[]" target="byte[]{$json} -> SetBytes"/>
    <type detected="java.lang.Object" target="interface{}{$json}"/>
</type-map>
func (dao *ProjectsDao) GetProjectList(ctx context.Context) (res []*dto.Project, err error) {
	sql := `select p.*,
		(select count(*) from tasks where p_id=p.p_id) as p_tasks_count
		from projects p
		order by p.p_id`
	errMap := make(map[string]int)
	_onRow := func(row map[string]interface{}) {
		obj := dto.Project{}
		SetInt64(&obj.PId, row, "p_id", errMap)
		SetString(&obj.PName, row, "p_name", errMap)
		SetInt64(&obj.PTasksCount, row, "p_tasks_count", errMap)
		res = append(res, &obj)
	}
	err = dao.ds.QueryAllRows(ctx, sql, _onRow)
	if err == nil {
		err = ErrMapToErr(errMap)
	}
	return
}
^ Automation of XML coding

By default, IDE XML editors provide code completion for XML-tags (CTRL+Space). The plug-ins extend this functionality with

  • code completion/navigation to select SQL files
  • code completion/navigation to select DTO classes
  • code completion to select data tables/views.
^ IDE toolbar button

Each plug-in extends IDE toolbar with a drop-down button for navigation between XML meta-programs.

Also, this button allows to generate/validate the code without opening plug-in GUI.

^ IDE Error Logging

There is a possibility to display errors of code generation/validation in IDE console. Update XSD files from the tab 'Admin' and add <ide event-log='true' /> to 'settings.xml'.

^ Code Validation Tools

1) Plug-in GUI > tab 'DTO' > local toolbar > 'Validate'. 2) Plug-in GUI > tab 'DAO' > local toolbar > 'Validate'.

These tools allow you to be sure (periodically) that all is OK with your XML meta-program, referenced SQL statements, and the code generated earlier. Errors and inconsistencies are logged and may be used for further analysis and planing of fixes.

^ Inheritance of generated DTO classes

'settings.xml':

<dto inheritance="implements java.io.Serializable" ...

Generated Java class:

public class Actor implements java.io.Serializable {

^ Top