Chapter 4. Conventions

SpringFuse has some built-in conventions. When these conventions are followed, SpringFuse generates cleaner Java code and some specific features. For example, if you want your users to upload some files through your web application, you can rely on SpringFuse to generate all the infrastructure code and configuration that will allow you to do it in an optimal way by simply following some columns naming convention.

4.1. Camel case conventions

4.1.1. Underscore '_' Enables Java Camel Case Syntax

'Camel Case' syntax is standard Java code convention. When SpringFuse encounters the character underscore '_' in a table’s name or a column’s name, it skips it and converts to upper case the next character when generating classes, variables or methods related to this table, or column.

Example 4.1. Basic conversion

For example, if your table name is BOOK_COMMENT , the generated model class will be named BookCommentModel ; a variable holding BookCommentModel instance will be named bookComment and a setter will be named setBookComment , etc.

4.1.2. Native camel case support

If your table's and/or column's name use a camelCase syntax AND if the JDBC driver preserves this syntax, then SpringFuse takes it into account when generating classes, variables or methods related to this table, or column.

Example 4.2. Example

For example, if your table name is bankAccount , the generated model class will be named BankAccountModel ; a variable holding BankAccountModel instance will be named bankAccount and a setter will be named setBankAccount , etc.

Choosing explicit names for your tables and columns is thus very important as it improves your source code readability.

4.2. Primary key conventions

4.2.1. Numerical Primary Keys

[Important]Important
Each numerical primary key is configured in the Hibernate mapping file with '<generator class="native"/>'. If your database does not support identity columns, you should create the sequence 'hibernate_sequence'. Please refer to Hibernate reference documentation for more advanced alternatives.

4.2.2. Primary Keys with 32 characters

By convention, for all primary keys that are char(32), SpringFuse configures Hibernate to use an UUIDHexGenerator. Therefore no sequence is needed for these primary keys.

4.2.3. Other Primary Keys

For primary key that are char(x) where x is different from 32, SpringFuse configures Hibernate with the "assigned" generator, which means you must provide manually the primary key value.

4.3. The 'Account' table

The Account table is a special table that contains the user's login and password columns and eventually the email and enabled columns. It has an important role during the login phase. It is also used by the AccountContext generated class which store the current account information in the current thread.

SpringFuse detects automatically your 'Account' table. An account table candidate is expected to have at least the following columns:

Table 4.1. Account table conditions

Column's nameMapped Java TypeDescription
"username" OR "login" OR "user_name" OR "identifiant"String Login used by the end user to authenticate to this web application
"password" OR "pwd" OR "passwd" OR "mot_de_passe" OR "motdepasse"String Password (in clear) used by the end user to authenticate to this web application

[Note]Note
If no Account table candidate is found, SpringFuse will do as if it had found one and will generate a mock Account DAO implementation that returns 2 dummy users (user/user and admin/admin) instead of generating an Hibernate DAO implementation. It is up to you to replace this DAO implementation with your own implementation.

4.4. The 'Role' table

The Role table is a special table that contains the account's roles. To be detected by SpringFuse, it must have a many-to-many relationship with the found 'Account' table and and contain the following mandatory column:

Table 4.2. Role table conditions

Column's nameMapped Java TypeDescription
"authority" OR "name_locale" OR "role_name" OR "role"String The generated code relies on the following authority's values: "ROLE_ANONYMOUS", "ROLE_USER", "ROLE_ADMIN"

Here is a sample SQL script (H2 Database) that complies to SpringFuse conventions

CREATE TABLE ACCOUNT (
    account_id char(32) not null, login
    varchar(255) not null,
    password varchar(255) not null,
    email varchar(255) not null,

    constraint account_unique_1 unique (login),
    constraint account_unique_2 unique (email),

    primary key (account_id) );

CREATE TABLE ROLE (
    role_id smallint generated by default as identity,
    name_locale varchar(255) not null,

    constraint role_unique_1 unique (name_locale),
    primary key (role_id) );

CREATE TABLE ACCOUNT_ROLE (
    account_id char(32) not null,
    role_id smallint not null,

    constraint account_role_fk_1 foreign key (account_id) references ACCOUNT,
    constraint account_role_fk_2 foreign key (role_id) references ROLE,

    primary key (account_id, role_id) );

4.5. Other optional account's columns

4.5.1. The Email column

If the detected Account table has an email column, it is used by the generated code in few places, mostly as an illustration of the EmailService usage.

Table 4.3. Account's table email column conditions

Column's nameMapped Java TypeDescription
"email", "email_address", "emailAddress", "mail"String The user's email.

4.5.2. The Enabled column

If the detected Account table has an enabled column, it is used by the generated code related to Spring Security integration.

Table 4.4. Account's table enabled column conditions

Column's nameMapped Java TypeDescription
"enabled" OR "is_enabled" OR "isenabled" Boolean Only enabled users (enabled == true) can login.

4.6. Special columns for file handling support

4.6.1. Columns group

When the following columns are present simultaneously in a table, SpringFuse generates various helper methods to ease file manipulation from the web tier to the persistence layer.

  • 'prefix'_FILE_NAME (String)

  • 'prefix'_CONTENT_TYPE (String)

  • 'prefix_SIZE or 'prefix'_LENGTH (int)

  • 'prefix'_BINARY (blob)

Example 4.3. 

mydoc_content_type       varchar(255)    not null,
mydoc_size               integer         not null,
mydoc_file_name          varchar(255)    not null,
mydoc_binary             bytea,

This convention will allow you to upload a file transparently, save it to the corresponding table, then download it using a simple URL.

4.7. _LOCALE column suffix & Localization

Whenever SpringFuse encounter a column name ending with '_LOCALE' it assumes that the columns is used to store a key for a localized property. The column type must naturally map to a java String (i.e database type of varchar). For such a column, SpringFuse generates a getter to retrieve directly the localized key’s value.

The locale used is retrieved from the Spring locale context.

[Note]Note
Make sure that the key used as a value in the 'xx_locale' column is also present in one of your properties file as a key with its* localized value.

Here is an example. You can add the TITLE_LOCALE column to your ACCOUNT table and fill it with either 'title.mister', 'title.miss' etc., SpringFuse will generate for you 2 additional methods in the AccountModel bean:

// returns the properties keys, stored in the database, for example: “title.mister”
String titleKey = getTitleLocale();

// returns the value “Monsieur” assuming that in a properties file you
// have defined for the French locale:
// title.mister=Monsieur
String titleValue = getTitleLocalized();

4.8. ACCOUNT_ID column & Hibernate Filter

When a table contains a foreign key pointing to the Account table, SpringFuse assumes that the content of this table belongs to the user represented by the account_id foreign key.

An hibernate filter is configured to make sure that this table is loaded only by the current user.

The filter is enabled by the ContextInterceptor. Of course this default behavior may not always suit your needs. There are two ways of disabling it:

  1. Disable the filter in the hibernate mapping file

  2. Disable the filter programmatically using the HibernateFilterContext generated helper.

4.9. Version column and Optimistic Locking

If your table has a column named VERSION whose type is an int, SpringFuse assumes by convention that you want to enable an optimistic locking strategy. As a result, the Hibernate mapping file generated by SpringFuse is configured by default to use this column for the optimistic locking strategy.

4.10. Many to many and inverse attribute

Which side of the relation is marked as inverse="true" ? By convention, the side whose corresponding column's order is the highest on the "Middle table".