A thorough configuration file example is provided when you create a project out of the sample H2 database that we provide.
The file is located under src/main/config/celerio-maven-plugin/celerio-maven-plugin.xml
.
This configuration file allows you to override conventions and control more precisely what is generated. For example, you may want to rename some variables, make some associations bidirectional, change a type, etc.
Here are the main configuration points:
Before editing your configuration file, make sure that Eclipse displays
the documentation present in the celerio.xsd
file and that it suggests
the available tags. From Eclipse, you cannot work efficiently without
the help of the XSD documentation.
If you rely on conventions, you do not need to configure anything regarding Ids. These examples are for advanced usage.
In case you use a sequence per table to generate your primary key values, you must configure Celerio in order to take it into account. Here is an example:
assuming the PK of the ADDRESS table is mapped to an Integer, here is how would look the generated code:
You can configure globally the sequence name pattern
In certain cases, generally when you work with legacy databases, you may need to use a custom Id generator in order to be consistent with the legacy system. Here is an example:
leads to:
‘Camel Case’ syntax is standard Java code convention. When Celerio
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.
For example, if your table name is BOOK_COMMENT
, the generated entity
class will be named BookComment
; a variable holding BookComment
instance will be named bookComment
and a setter will be named
setBookComment
, etc.
If your table’s and/or Column use a camel case syntax and if the JDBC driver preserves this syntax, then Celerio takes it into account when generating classes, variables or methods related to this table, or column.
For example, if your table name is bankAccount
, the generated entity
class will be named BankAccount
; a variable holding BankAccount
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 without the burden of relying on configuration.
By default, an entity name is deduced from the Table name. To force the
entity name to a different value, use the entityName
attribute of the
entityConfig
element, for example.
By default, a property name is deduced from the column name. To force
the property name to a different value, use the fieldName
attribute of
the columnConfig
element, for example.
leads to:
By default Celerio calculates Java entity/field name based on the underlying table/column name.
If your database schema has some naming convention using prefixes, this can lead to non friendly entity/column name.
Hopefully, you can configure Celerio to pre-process all your table names and column names before applying its convention to compute the entity/column name.
Here are some configuration examples:
In that case, assuming we have a table named tbl_account
, the entity will be named Account
instead of TblAccount
.
Column names such as XYZ_SOMETHING_MEANINGFUL
will give sometingMeaningful
instead of xyzSometingMeaningful
.
Celerio has some conventions regarding type mapping. You can change them either locally or globally using rules.
You can force the mapped type using the mappedType
attribute of the
columnConfig element. For example to force a mapping to an Integer you
would do:
You can configure number mapping rules globally. For example, to map all columns whose size and decimal digits are > 1 to BigDecimal, proceed as follow:
First rule that matches is used. For example to map to either Boolean, Double or BigDecimal you can do:
Note that the columnSizeMin
is inclusive and columnSizeMax
is
exclusive.
You can configure date mapping rules globally. For example, to map all date/time/timestamp column to Joda Time LocalDateTime, proceed as follow:
And for example to map differently the columns whose name is VERSION you can add the following mapping rule:
To map a column to an Java Enum using the @Enumerated
value, see:
To map your enum to a value that is different from the value returned by either the enum’s name() or enum’s ordinal() method, see:
In this example, the persisted value is the value returned by the enum’s name() method.
Value persisted in the database: Either “MR” or “MS”.
Celerio configuration:
You can configure multiple languages too
Generated code:
In this example, the persisted value is the value returned by the enum’s ordinal() method.
Value persisted in the database: Either 0 (for ‘MR’) or 1 (for ‘MS’).
Celerio configuration:
Generated code:
This mapping allows you to customize the persisted value instead of relying on the name() or ordinal() enum’s method.
Value persisted in the database: Either “MR” or “MS”.
Celerio configuration:
Generated code:
By default, Celerio generates the code for a @ManyToOne
association
when it encounters a column having a foreign key
constraint and no
unique
constraint.
The variable name of the many to one association is deduced by default
from the fieldName
of the column. For example if the fieldName
is
addressId
, the many to one variable name will be address
. In case
where the fieldName
already matches the name of the target entity,
Celerio adds the “Ref” suffix to the variable name. Here are few
simplified examples:
In any case, use the manyToOneConfig
element to force a different
variable name. For example:
will lead to
The manyToOneConfig
element also allows you to tune the JPA fetch type
and the JPA cascade types. Please refer to the XSD for more information.
If you have some inheritance involved on the ‘one’ side of the many to
one association, the table referenced by the foreign key is not enough
to identify the target entity. In that case, set the targetEntityName
attribute of the columnConfig
element. For example:
On legacy schema, the foreign key constraint may not be present and
Celerio will not generate the many to one association you would expect.
Hopefully you can configure Celerio to do as if a foreign key constraint
was present by setting the targetTableName
attribute of the
columnConfig
element. For example:
One to many association is configured on the ‘many’ side of the
association, more precisely on the same columnConfig
as the one used
for the associated many to one association. This may be a bit confusing
at first but it has the advantage to group together, both associations
on the side that really owns the association.
Celerio generates the code for one to many association when a many to
one association is present and when the associationDirection
attribute
of the columnConfig
element is BIDIRECTIONAL
. For example:
will lead (assuming address_id refers to Address) to something like:
In the example above accounts
is simply the plural of the Account
entity that Celerio guessed. We were of course lucky on this one.
Use the oneToManyConfig
element of the columnConfig
to set the
name of the one to many association to a different value. As you will
see, you can also set the name of an element of the collection to
control the name of the associated helper methods that Celerio generates
(adder, remover, etc.). Here is an example:
will lead to
The oneToManyConfig
element also allows you to tune the JPA fetch type
and the JPA cascade types. Please refer to the XSD for more information.
By default, Celerio generates the code for a @ManyToOne
association
when it encounters a composite foreign key.
The manyToOneConfig
should be a child of the columnConfig corresponding to the first column of the composite foreign key.
As for regular @OneToMany, you can use the oneToManyConfig
element to generate the inverse association.
Do not forget to set the associationDirection
attribute of the columnConfig
element to BIDIRECTIONAL
By default, Celerio generates the code for a @OneToOne
association
when it encounters a column having a foreign key
constraint AND a
unique
constraint.
One to one association configuration is very similar to many to one association.
To change the variable name, the JPA fetch type or the cascade types of the
one to one association, use the oneToOneConfig
element of the columnConfig
element. Here is an example:
will lead to
Inverse one to one association is for one to one association what one to many association is for many to one association.
Celerio generates the code for inverse one to one association when a one
to one association is present and when the associationDirection
attribute of the columnConfig
element is BIDIRECTIONAL
.
Inverse one to one association is configured on the owning side of
association, that is on the columnConfig
that has the foreign key and
unique constraints.
As for one to many association configuration, this may be a bit confusing at first but it has the advantage to group together, both associations on the side that really owns the association.
To change the variable name, the JPA fetch type or the cascade types of the
inverse one to one association, use the inverseOneToOneConfig
element of the columnConfig
element. Here is an example:
Here is an example:
will lead to
Many to many association necessarily involves a join table. When Celerio detects a join table, it generates the code for the many to many relation. Celerio assumes that a table is a join table when it has 2 foreign keys and no other columns, except eventually a primary key column and a column used for optimistic locking.
To fine tune the many to many association, you must declare an
entityConfig for the join table. You may use the manyToManyConfig
element. to set the related variables and adder/remover/etc. method
names. You can use the inverse
attribute to force the inverse side of
the association. For example:
In case Celerio does not detect the join table, for example if an
extra column is present, you can force it by setting to true
the
middleTable
attribute of the entityConfig
element.
Which side of the @ManyToMany relation is marked as inverse="true"
?
By convention, the side whose corresponding column’s order is the highest on the ‘middle table’. You can override this convention using the parent’s columnConfig’s ‘inverse’ attribute.
Since Celerio 3.0.101
Configuration is very similar to many to many configuration above except that you should use the manyToOneConfig
element for the forward association and
the OneToManyConfig
element for the inverse association. For example:
By default Celerio does not try to guess any inheritance strategy.
However, you can configure Celerio to take your inheritance strategy into account.
The SINGLE_TABLE
strategy maps all entities in the hierarchy to the same table.
Please find below a schema example and the corresponding Celerio configuration.
Here is the corresponding SINGLE_TABLE
inheritance configuration:
The JOINED
strategy uses a different table for each entity in the hierarchy.
Please find below a schema example and the corresponding Celerio configuration.
Here is the corresponding JOINED
inheritance configuration:
This feature is not implemented
© 2005-2015 Jaxio | @jaxiosoft | Legal