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 SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionintdelete()Removes the model's instance from database.booleanexists()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 StringgetColumnName(Field modelField) Builds a column name from a model field.static StringgetColumnNameFromFieldName(Class<? extends Model> modelClass, String fieldName) Get the corresponding field name of a column for a defined model class.static StringgetFieldNameFromColumnName(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 PropertyDescriptorgetPropertyDescriptor(Class<? extends Model> modelClass, String fieldName) Get a field's property descriptor for a defined model class.static StringgetTableName(Class<?> modelClass) Builds a table name from a model class.static OrderByorderByIDs(Class<? extends Model> modelClass) Get an Order By clause to order by a model's IDs in ascending order.intsave()Saves a model instance to database, using the model's attribute values.booleanwhereByIds(Where whereClause) Updates a Where clause to select the current model.
- 
Constructor Details- 
Modelpublic Model()
 
- 
- 
Method Details- 
getTableNameBuilds a table name from a model class.- Parameters:
- modelClass- Model class
- Returns:
- Model's table name
 
- 
getColumnNameBuilds a column name from a model field.- Parameters:
- modelField- Model field
- Returns:
- Field's column name or nullif field should be excluded
 
- 
getFieldNameFromColumnNamepublic 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 class
- columnName- Column name
- Returns:
- Column's name field name or nullif field should be excluded
 
- 
getColumnNameFromFieldNamepublic 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 class
- fieldName- Field name
- Returns:
- Field's column name or nullif field should be excluded
 
- 
getColumnFieldNamesGet all the column/field name pairs for a defined model class.- Parameters:
- modelClass- Model class
- Returns:
- Column/field name pairs
 
- 
getFieldNamesGet all the field names for a defined model class.- Parameters:
- modelClass- Model class
- Returns:
- Column/field name pairs
 
- 
getPropertyDescriptorpublic static PropertyDescriptor getPropertyDescriptor(Class<? extends Model> modelClass, String fieldName) Get a field's property descriptor for a defined model class.- Parameters:
- modelClass- Model class
- fieldName- Field to use
- Returns:
- Field's property descriptor
 
- 
orderByIDsGet an Order By clause to order by a model's IDs in ascending order.- Parameters:
- modelClass- Model class
- Returns:
- Order By clause
 
- 
getIdFieldNamesReturns 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
 
- 
whereByIdsUpdates a Where clause to select the current model.- Parameters:
- whereClause- Where clause to update
- Returns:
- trueWhere clause updated with success
- Throws:
- NullPointerException- Where clause is null
 
- 
saveSaves 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
 
- 
deleteRemoves 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
 
- 
existsTells if the model instance already exists in the database. Returnstrueif a superclass instance exists.- Returns:
- trueModel instance exist in database.
- Throws:
- DatabaseException
 
 
-