Models
Concepts
Entity
A representation of a table in a programatic way
Entity registry
Mandarine keeps a list of all the entities that your application contains. This list is called Entity Registry as it works as a registry for Mandarine's engine to interact with your entities.
Database interaction
Your tables (entities) as well as the columns of those tables are created automatically in the database if they do not exist. This process happens at mandarine compile time.
If one of your entities is already in the database, but you have added a new column, Mandarine will resolve the new column and will add it to your table in your database.
Declaring an entity
An entity is declared by using the decorator @Table
. This decorator will add your virtual object database to the entity registry to then be used across your application.
The table decorator targets a class.
Syntax:
name
Optional
If name is not defined, the name of the target class will be considered the table's name
schema
Required
Schema where your table will be located
Declaring a column
In order to declare a column for your entity, you will need to use the decorator @Column
.
The @Column
decorator targets a class field/property.
Syntax:
name
: Name of the columntype
: The reference of a SQL type given by a mandarine internal enum (See enum here). Note that if your current dialect does not recognize the indicated value, then the value will be set to character varyingIf the type of the variable is: numeric, string, boolean. Mandarine will automatically read & detect its type
unique
: Specifies whether the column will have an unique constraintnullable
: Specifies whether the column can be nulllength
: Specifies the length of the column (used in certain cases, such as when the column's type is character varying/varchar)precision
: Specifies the precision of a numeric character (used in certain cases, such as when the type is decimal)scale
: Specifies the scale of the numeric character (used in certain cases, such as when the type is decimal)
Declaring a primary key
The declaration of a primary key depends on two decorators, the decorator @Id
and the decorator @GeneratedValue
. Both decorators target a class field previously decorated with the decorator @Column
@Id
will specify that a column is a primary key@GeneratedValue
will specify the type of strategy to be used for the declared primary key
Syntax:
Declares what strategy the table's primary key will use.
If
strategy
is SEQUENCE, then the column is assumed to be serial (auto-increment)If
strategy
is MANUAL, then when an insert is invoked through Mandarine's ORM, the value will be generated by invokingmanualHandler
.
Creating a model
Now that we have seen the concepts involved in Models, we can create one.
In the example above we are declaring an entity that will have Users as name, it has 4 columns: id, firstname, lastname, country. The column id has been annotated with ID, and GeneratedValue, meaning id will be a primary key and its strategy is SEQUENCE/Auto-increment. Note that if your model has a constructor, ALL its parameters must be optional.
Last updated