Class Model
java.lang.Object
ece.ing3.java.projet.database.sql.Model
Abstract database model helper.
Models should inherit from this class to be able to use other helpers.
A model is an object that represent a database entry.
Table name is the model's lowercase class name.
Each field is automatically mapped to a column, if it is not excluded with the ExcludedField
annotation.
A field's corresponding column name is either described by a Column
annotation or by using directly the lowercase field name.
A field can be described as being used as primary key in database using the Id
annotation.
Each model must have a default constructor, getters and setters for each field. Failing in following this rule will lead to undefined behavior.
One example of model can be :
public class ExampleModel extends Model { @Id @Column( name = 'id' ) private Long exampleId; // Field being a database ID, named exampleId and mapped to column 'id' private String exampleString; // Field name exampleString, mapped to column 'examplestring' @ExcludedField private String excludedString; // Not used as a database column public ExampleModel() {} // ... getters and setters for exampleId and exampleString ... }
- Author:
- Virgile, Nicolas, Louis-Félix
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
delete()
Removes the model's instance from database.boolean
exists()
Tells if the model instance already exists in the database.getColumnFieldNames
(Class<? extends Model> modelClass) Get all the column/field name pairs for a defined model class.static String
getColumnName
(Field modelField) Builds a column name from a model field.static String
getColumnNameFromFieldName
(Class<? extends Model> modelClass, String fieldName) Get the corresponding field name of a column for a defined model class.static String
getFieldNameFromColumnName
(Class<? extends Model> modelClass, String columnName) Get the corresponding field name of a column for a defined model class.static String[]
getFieldNames
(Class<? extends Model> modelClass) Get all the field names for a defined model class.static String[]
getIdFieldNames
(Class<? extends Model> modelClass) Returns a list of all the ID fields's names for a defined model class.static PropertyDescriptor
getPropertyDescriptor
(Class<? extends Model> modelClass, String fieldName) Get a field's property descriptor for a defined model class.static String
getTableName
(Class<?> modelClass) Builds a table name from a model class.static OrderBy
orderByIDs
(Class<? extends Model> modelClass) Get an Order By clause to order by a model's IDs in ascending order.int
save()
Saves a model instance to database, using the model's attribute values.boolean
whereByIds
(Where whereClause) Updates a Where clause to select the current model.
-
Constructor Details
-
Model
public Model()
-
-
Method Details
-
getTableName
Builds a table name from a model class.- Parameters:
modelClass
- Model class- Returns:
- Model's table name
-
getColumnName
Builds a column name from a model field.- Parameters:
modelField
- Model field- Returns:
- Field's column name or
null
if field should be excluded
-
getFieldNameFromColumnName
public static String getFieldNameFromColumnName(Class<? extends Model> modelClass, String columnName) Get the corresponding field name of a column for a defined model class.- Parameters:
modelClass
- Model classcolumnName
- Column name- Returns:
- Column's name field name or
null
if field should be excluded
-
getColumnNameFromFieldName
public static String getColumnNameFromFieldName(Class<? extends Model> modelClass, String fieldName) Get the corresponding field name of a column for a defined model class.- Parameters:
modelClass
- Model classfieldName
- Field name- Returns:
- Field's column name or
null
if field should be excluded
-
getColumnFieldNames
Get all the column/field name pairs for a defined model class.- Parameters:
modelClass
- Model class- Returns:
- Column/field name pairs
-
getFieldNames
Get all the field names for a defined model class.- Parameters:
modelClass
- Model class- Returns:
- Column/field name pairs
-
getPropertyDescriptor
public static PropertyDescriptor getPropertyDescriptor(Class<? extends Model> modelClass, String fieldName) Get a field's property descriptor for a defined model class.- Parameters:
modelClass
- Model classfieldName
- Field to use- Returns:
- Field's property descriptor
-
orderByIDs
Get an Order By clause to order by a model's IDs in ascending order.- Parameters:
modelClass
- Model class- Returns:
- Order By clause
-
getIdFieldNames
Returns a list of all the ID fields's names for a defined model class.- Parameters:
modelClass
- Model class- Returns:
- List of ID fields's names
-
whereByIds
Updates a Where clause to select the current model.- Parameters:
whereClause
- Where clause to update- Returns:
true
Where clause updated with success- Throws:
NullPointerException
- Where clause is null
-
save
Saves a model instance to database, using the model's attribute values.- Returns:
- Either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
- Throws:
DatabaseException
- Error occurred while saving to database
-
delete
Removes the model's instance from database.- Returns:
- Either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
- Throws:
DatabaseException
- Error occurred while deleting from database
-
exists
Tells if the model instance already exists in the database. Returnstrue
if a superclass instance exists.- Returns:
true
Model instance exist in database.- Throws:
DatabaseException
-