With MentaBean you do the object-to-database mapping with Java code. Check the example below:
BeanManager mgr = new BeanManager(); BeanConfig userCfg = new BeanConfig(User.class, "Users"); // "Users" is the table name userCfg.pk("id", DBTypes.AUTOINCREMENT); // the primary key! userCfg.field("username", DBTypes.STRING); // the database column will have the same of the property name: "username" userCfg.field("birthdate", "bd", DBTypes.DATE); // note that the database column name is different userCfg.field("status", DBTypes.ENUMVALUE.from(Status.class)); // status will be saved as a varchar in the database userCfg.field("deleted", DBTypes.BOOLEANINT); // 0 for false and 1 for true in the database userCfg.field("insertTime", "insert_time", DBTypes.NOW_ON_INSERT_TIMESTAMP); // again database column name is different than property name mgr.addBeanConfig(userCfg);
Or same thing using the Fluent API:
BeanManager mgr = new BeanManager(); BeanConfig userCfg = new BeanConfig(User.class, "Users") // "Users" is the table name .pk("id", DBTypes.AUTOINCREMENT) // the primary key! .field("username", DBTypes.STRING) // the database column will have the same of the property name: "username" .field("birthdate", "bd", DBTypes.DATE) // note that the database column name is different .field("status", DBTypes.ENUMVALUE.from(Status.class)) // status will be saved as a varchar in the database .field("deleted", DBTypes.BOOLEANINT) // 0 for false and 1 for true in the database .field("insertTime", "insert_time", DBTypes.NOW_ON_INSERT_TIMESTAMP); // again database column name is different than property name mgr.addBeanConfig(userCfg);
Or same thing using the PropertiesProxy:
BeanManager mgr = new BeanManager(); User userProps = PropertiesProxy.create(User.class); BeanConfig userCfg = new BeanConfig(User.class, "Users") .pk(userProps.getId(), DBTypes.AUTOINCREMENT) .field(userProps.getUsername(), DBTypes.STRING) .field(userProps.getBirthdate(), "bd", DBTypes.DATE) // note that the database column name is different .field(userProps.getStatus(), DBTypes.ENUMVALUE.from(Status.class)) .field(userProps.isDeleted(), DBTypes.BOOLEANINT) .field(userProps.getInsertTime(), "insert_time", DBTypes.NOW_ON_INSERT_TIMESTAMP); mgr.addBeanConfig(userCfg);
MentaBean comes with many ready-to-use field types (DBTypes) and you can also create your owns. Below we explain each one of them:
Straightforward fields that map Java numbers to database numbers. They are:
NOTE: If a number in the database has the value NULL and you want to reflect this NULL value in your bean properties, then you should make a property that is a primitive Wrapper (i.e. java.lang.Integer). If the property is a primitive (i.e. int) a NULL value in the database will be converted to a ZERO value in the property when the bean is loaded.
Databases usually store boolean values as tiny integers (0 and 1) or enumerations ('T' and 'F'). MentaBean supports both of them through the:
By default, the BOOLEANSTRING type assumes 'T' and 'F' as true and false, but you can easily change that to any value you want like the example below:
userCfg.field("deleted", DBTypes.BOOLEANSTRING.values("Yes", "No"));
NOTE: If the your database has a function to generate the current timestamp (i.e. now(), sysdate, etc.) it will be used, so the current timestamp will be the one from the database machine. Otherwise a new java.util.Date is passed as the current timestamp.
If the database column is configured as an auto-increment field, you can use the AutoIncrementType so that the field is not included in the insert query.
NOTE: MentaBean automatically populates an auto-increment property in the bean with the id generated by the database so after an insert is performed you can easily call the getter of the property to find out the id generated.
Some databases like Oracle support a sequence type. If you have a sequence created in your database you can configure a property in your bean to use that sequence with the field type below:
By default, MentaBean assumes the following name for the sequence: seq_<DB_COLUMN_NAME>_<DB_TABLE_NAME>. However you can specify the sequence name yourself using the fluent API like below:
.pk("id", DBTypes.SEQUENCE).seq("seq_myseq_in_database")
You can save an Enumeration property in the database with its string value or with its id by using one of the field types below:
For both of these types you have to use the fluent API to pass the enumeration, like below:
// Status.class is your enumeration config.field("status", DBTypes.ENUMVALUE.from(Status.class)); config.field("status", DBTypes.ENUMID.from(Status.class));
To use the ENUMID type, your enumeration must implement two methods: getId() and fromId(). Below you can see an example:
public static enum Test { T1(1), T2(2), T3(3); private final int id; private Test(int id) { this.id = id; } public int getId() { return id; } public static Test fromId(int id) { for(Test t : Test.values()) { if (t.getId() == id) return t; } return null; } }
NOTE: The fromId() method must be static.
The database types provided by MentaBean should be sufficient for most of your fields, but you can also code your own types by implementing the DBType interface.