Parcourir la source

Refactorisation code pour ameliorer maintenabilité

Arthur Brandao il y a 5 ans
Parent
commit
3dc22e2d7d

+ 2 - 8
src/db/Database.java

@@ -97,10 +97,7 @@ public class Database {
 		try(Statement st = db.createStatement()) {
 			ResultSet result = st.executeQuery(sql);
 			T obj = mapper.map(result);
-			if (obj == null) {
-				return Optional.empty();
-			}
-			return Optional.of(obj);
+			return (obj == null) ? Optional.empty() : Optional.of(obj);
 		} catch (SQLException | IllegalStateException e) {
 			LOGGER.warning(e.getMessage());
 			return Optional.empty();
@@ -115,10 +112,7 @@ public class Database {
 			}
 			ResultSet result = st.executeQuery();
 			T obj = mapper.map(result);
-			if (obj == null) {
-				return Optional.empty();
-			}
-			return Optional.of(obj);
+			return (obj == null) ? Optional.empty() : Optional.of(obj);
 		} catch (SQLException | IllegalStateException e) {
 			LOGGER.warning(e.getMessage());
 			return Optional.empty();

+ 107 - 157
src/db/DatabaseTable.java

@@ -64,6 +64,9 @@ public abstract class DatabaseTable<T extends Persistable> {
 	}
 	
 	public T refresh(long id) {
+		if (!DatabaseProperties.getBool("cache")) {
+			throw new IllegalArgumentException("Cache is not enabled, can't refresh");
+		}
 		if (!cacheMap.containsKey(id)) {
 			throw new IllegalArgumentException("Entity is not load, can't refresh");
 		}
@@ -79,46 +82,34 @@ public abstract class DatabaseTable<T extends Persistable> {
 	}
 	
 	public T save(T obj) {
-		if (cacheMap.containsKey(obj.getId())) {
-			update(obj);
-		} else {
-			insert(obj);
-		}
-		return cache(obj);
-	}
-	
-	public void delete(long id) {
-		DbTable dbTable = getDbTableAnnotation();
-		// Cherche l'id
-		String idField = null;
-		for(Field field : dbTable.entity().getFields()) {
-			if(!field.isAnnotationPresent(DbId.class)) {
-				continue;
+		// Si le cache est activé on se base dessus
+		if (DatabaseProperties.getBool("cache")) {
+			if (cacheMap.containsKey(obj.getId())) {
+				update(obj);
+			} else {
+				insert(obj);
 			}
-			// Lecture du nom de l'id dans la base
-			if(field.isAnnotationPresent(DbField.class)) {
-				idField = field.getAnnotation(DbField.class).value();
+		} 
+		// Sinon on regarde en base
+		else {
+			if (findById(obj.getId()).isPresent()) {
+				update(obj);
 			} else {
-				throw new IllegalStateException("Unable to find DbField annotation on id");
+				insert(obj);
 			}
 		}
-		if (idField == null) {
-			throw new IllegalStateException("Unable to find find DbField annotation");
-		}
-		// Requete SQL
-		String sql = "Delete From " + dbTable.name() + " Where " + idField + " = ?";
-		List<Object> params = new ArrayList<>();
-		params.add(id);
-		// Execution requete
-		if (!Database.execute(sql, params)) {
-			throw new IllegalStateException("Unable to delete data");
-		}
+		return cache(obj);
+	}
+	
+	public void del(long id) {
+		// Suppr de la base
+		delete(id);
 		// Retire du cache
 		remove(id);
 	}
 	
-	public void delete(T obj) {
-		delete(obj.getId());
+	public void del(T obj) {
+		del(obj.getId());
 	}
 	
 	private T cache(T obj) {
@@ -152,27 +143,15 @@ public abstract class DatabaseTable<T extends Persistable> {
 	
 	private Optional<T> getFromDbById(long id) {
 		DbTable dbTable = getDbTableAnnotation();
-		// Cherche l'id
-		String idField = null;
-		for(Field field : dbTable.entity().getFields()) {
-			if(!field.isAnnotationPresent(DbId.class)) {
-				continue;
-			}
-			// Lecture du nom de l'id dans la base
-			if(field.isAnnotationPresent(DbField.class)) {
-				idField = field.getAnnotation(DbField.class).value();
-			} else {
-				throw new IllegalStateException("Unable to find DbField annotation on id");
-			}
-		}
+		// Recupère l'id
+		Field idField = DatabaseUtils.getIdField(dbTable.entity());
 		if (idField == null) {
-			throw new IllegalStateException("Unable to find find DbField annotation");
+			throw new IllegalStateException("Unable to find id field");
 		}
 		// Requete sql
-		String sql = "Select * From " + dbTable.name() + " Where " + idField + " = ?";
-		List<Object> params = new ArrayList<>();
-		params.add(id);
-		Optional<T> result = Database.query(sql, params, DatabaseMapper.objectMapper(dbTable));
+		SQLQueryBuilder sql = SQLQueryBuilder.selectQuery(dbTable.name());
+		sql.add(DatabaseUtils.getDbField(idField), id);
+		Optional<T> result = Database.query(sql.toString(), sql.getParams(), DatabaseMapper.objectMapper(dbTable));
 		// Return
 		return result;
 	}
@@ -180,10 +159,9 @@ public abstract class DatabaseTable<T extends Persistable> {
 	private List<T> getFromDbByField(String fieldname, Object value) {
 		DbTable dbTable = getDbTableAnnotation();
 		// Requete sql
-		String sql = "Select * From " + dbTable.name() + " Where " + fieldname + " = ?";
-		List<Object> params = new ArrayList<>();
-		params.add(value);
-		Optional<List<T>> result = Database.query(sql, params, DatabaseMapper.listMapper(dbTable));
+		SQLQueryBuilder sql = SQLQueryBuilder.selectQuery(dbTable.name());
+		sql.add(fieldname, value);
+		Optional<List<T>> result = Database.query(sql.toString(), sql.getParams(), DatabaseMapper.listMapper(dbTable));
 		// Return
 		if(result.isPresent()) {
 			return result.get();
@@ -194,9 +172,8 @@ public abstract class DatabaseTable<T extends Persistable> {
 	private List<T> getWhereFromDb(List<String> where, List<Object> params) {
 		DbTable dbTable = getDbTableAnnotation();
 		// Requete sql
-		StringBuilder sql = new StringBuilder();
-		sql.append("Select * From " + dbTable.name() + "Where 1=1");
-		where.forEach(elt -> sql.append(" And " + elt + " = ?"));
+		SQLQueryBuilder sql = SQLQueryBuilder.selectQuery(dbTable.name());
+		where.forEach(elt -> sql.add(elt));
 		// Execution requete
 		Optional<List<T>> result = Database.query(sql.toString(), params, DatabaseMapper.listMapper(dbTable));
 		// Return
@@ -209,8 +186,8 @@ public abstract class DatabaseTable<T extends Persistable> {
 	private List<T> getAllFromDb() {
 		DbTable dbTable = getDbTableAnnotation();
 		// Requete sql
-		String sql = "Select * From " + dbTable.name();
-		Optional<List<T>> result = Database.query(sql, DatabaseMapper.listMapper(dbTable));
+		SQLQueryBuilder sql = SQLQueryBuilder.selectQuery(dbTable.name());
+		Optional<List<T>> result = Database.query(sql.toString(), DatabaseMapper.listMapper(dbTable));
 		// Return
 		if(result.isPresent()) {
 			return result.get();
@@ -220,67 +197,39 @@ public abstract class DatabaseTable<T extends Persistable> {
 	
 	private void insert(T obj) {
 		DbTable dbTable = getDbTableAnnotation();
-		// Requete SQL
-		StringBuilder sql = new StringBuilder();
-		StringBuilder values = new StringBuilder();
-		String dbIdName = null; // Nom de l'id en base
-		String attrIdName = null; // Nom de l'id dans la class
-		List<Object> params = new ArrayList<>();
-		sql.append("Insert Into " + dbTable.name() + "(");
-		boolean first = true;
+		// Récupération du champ id
+		Field id = DatabaseUtils.getIdField(obj.getClass());
+		if (id == null) {
+			throw new IllegalStateException("Unable to find id field");
+		}
 		try {
+			// Création requete SQL
+			SQLQueryBuilder sql = SQLQueryBuilder.insertQuery(dbTable.name());
 			for(Field field : obj.getClass().getFields()) {
-				if (!field.isAnnotationPresent(DbField.class)) {
-					continue;
-				}
-				// Cherche l'id
-				if (dbIdName == null && field.isAnnotationPresent(DbId.class)) {
-					dbIdName = field.getAnnotation(DbField.class).value();
-					attrIdName = field.getName();
+				String dbField;
+				if ((dbField = DatabaseUtils.getDbField(field)) == null || DatabaseUtils.isDbId(field)) {
 					continue;
 				}
-				// Recupere la valeur des autres attributs
-				if (!first) {
-					sql.append(",");
-					values.append(",");
+				Object value = field.get(obj);
+				// Si c'est un lien vers une autre entité
+				if (value != null && DatabaseUtils.isDbLink(field)) {
+					// Recup class gestion de la table pour sauvegarder l'objet avant de l'ajouter à la requete
+					Persistable link = DatabaseUtils.getDbLinkObject(field, obj);
+					DatabaseTable dt = DatabaseUtils.getDatabaseTable(field);
+					dt.save(link);
+					value = link.getId();
 				}
-				sql.append(field.getAnnotation(DbField.class).value());
-				values.append("?");
-				// Si il y a un lien avec une autre entité
-				if(field.isAnnotationPresent(DbLink.class)) {
-					DatabaseUtils.checkIfPersistable(field);
-					if(field.get(obj) == null) {
-						params.add(null);
-					} else {
-						Persistable persistable = (Persistable) field.get(obj);
-						// TODO Sauvegarder l'autre objet avant
-						params.add(persistable.getId());
-					}
-				}
-				// Sinon ajoute simplement le parametre
-				else {
-					params.add(field.get(obj));
-				}
-				first = false;
+				sql.add(dbField, value);
 			}
-		} catch (IllegalArgumentException | IllegalAccessException e) {
-			throw new IllegalStateException("Unable to access to the attribute", e);
-		}	
-		sql.append(") Values (");
-		sql.append(values);
-		sql.append(")");
-		// Verif que l'on a bien le nom de l'id
-		if(dbIdName == null) {
-			throw new IllegalStateException("Unable to find id field");
-		}
-		// Execution de la requete
-		Map<String, Object> newId = Database.insert(sql.toString(), params, new String[]{dbIdName});
-		if (newId.isEmpty()) {
-			throw new IllegalStateException("Unable to save data");
-		}
-		// Application nouvelle id
-		Object val = newId.get(dbIdName);
-		try {
+			// Execution de la requete
+			String dbIdName = DatabaseUtils.getDbField(id);
+			String attrIdName = id.getName();
+			Map<String, Object> newId = Database.insert(sql.toString(), sql.getParams(), new String[]{dbIdName});
+			if (newId.isEmpty()) {
+				throw new IllegalStateException("Unable to save data");
+			}
+			// Récupération de l'id
+			Object val = newId.get(dbIdName);
 			if (val instanceof BigDecimal) {
 				BigDecimal b = (BigDecimal) val;
 				obj.getClass().getField(attrIdName).set(obj, b.longValue());
@@ -288,59 +237,60 @@ public abstract class DatabaseTable<T extends Persistable> {
 				obj.getClass().getField(attrIdName).set(obj, val);
 			}
 		} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
-			throw new IllegalStateException("Unable to set id", e);
+			throw new IllegalStateException("Unable to save data", e);
 		}
 	}
 	
 	private void update(T obj) {
 		DbTable dbTable = getDbTableAnnotation();
-		// Requete SQL
-		StringBuilder sql = new StringBuilder();
-		StringBuilder where = new StringBuilder();
-		List<Object> params = new ArrayList<>();
-		sql.append("Update " + dbTable.name() + " Set ");
-		where.append(" Where ");
-		boolean first = true;
-		for (Field field : obj.getClass().getFields()) {
-			try {
-				if (!field.isAnnotationPresent(DbField.class)) {
-					continue;
-				}
-				// Si c'est l'id
-				if (field.isAnnotationPresent(DbId.class)) {
-					where.append(field.getAnnotation(DbField.class).value() + " = ?");
+		// Récupération du champ id
+		Field id = DatabaseUtils.getIdField(obj.getClass());
+		if (id == null) {
+			throw new IllegalStateException("Unable to find id field");
+		}
+		try {
+			// Création requete SQL
+			SQLQueryBuilder sql = SQLQueryBuilder.updateQuery(dbTable.name());
+			for(Field field : obj.getClass().getFields()) {
+				String dbField;
+				if ((dbField = DatabaseUtils.getDbField(field)) == null || DatabaseUtils.isDbId(field)) {
 					continue;
 				}
-				// Sinon ajoute dans le set
-				if (!first) {
-					sql.append(",");
-				}
-				sql.append(field.getAnnotation(DbField.class).value() + " = ?");
-				// Si il y a un lien avec une autre entité
-				if(field.isAnnotationPresent(DbLink.class)) {
-					DatabaseUtils.checkIfPersistable(field);
-					if(field.get(obj) == null) {
-						params.add(null);
-					} else {
-						Persistable persistable = (Persistable) field.get(obj);
-						// TODO Sauvegarder l'autre objet avant
-						params.add(persistable.getId());
-					}
+				Object value = field.get(obj);
+				// Si c'est un lien vers une autre entité
+				if (value != null && DatabaseUtils.isDbLink(field)) {
+					// Recup class gestion de la table pour sauvegarder l'objet avant de l'ajouter à la requete
+					Persistable link = DatabaseUtils.getDbLinkObject(field, obj);
+					DatabaseTable dt = DatabaseUtils.getDatabaseTable(field);
+					dt.save(link);
+					value = link.getId();
 				}
-				// Sinon ajoute simplement le parametre
-				else {
-					params.add(field.get(obj));
-				}
-				first = false;
-			} catch (IllegalArgumentException | IllegalAccessException e) {
-				throw new IllegalStateException("Unable to access to the attribute", e);
+				sql.add(dbField, value);
+			}
+			// Ajoute l'id
+			sql.addId(DatabaseUtils.getDbField(id), id.get(obj));
+			// Execution de la requete
+			if(!Database.execute(sql.toString(), sql.getParams())) {
+				throw new IllegalStateException("Unable to save data");
 			}
+		} catch (IllegalArgumentException | IllegalAccessException | SecurityException e) {
+			throw new IllegalStateException("Unable to save data", e);
 		}
-		sql.append(where);
-		params.add(obj.getId());
-		// Execution requete
-		if(!Database.execute(sql.toString(), params)) {
-			throw new IllegalStateException("Unable to save data");
+	}
+	
+	private void delete(long id) {
+		DbTable dbTable = getDbTableAnnotation();
+		// Récupération du champ id
+		Field idField = DatabaseUtils.getIdField(dbTable.entity());
+		if (idField == null) {
+			throw new IllegalStateException("Unable to find id field");
+		}
+		// Création requete SQL
+		SQLQueryBuilder sql = SQLQueryBuilder.deleteQuery(dbTable.name());
+		sql.addId(DatabaseUtils.getDbField(idField), id);
+		// Execution de la requete
+		if (!Database.execute(sql.toString(), sql.getParams())) {
+			throw new IllegalStateException("Unable to delete data");
 		}
 	}
 

+ 201 - 0
src/db/SQLQueryBuilder.java

@@ -0,0 +1,201 @@
+package db;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+public class SQLQueryBuilder {
+	
+	private static final int SELECT_QUERY = 0;
+	private static final int INSERT_QUERY = 1;
+	private static final int UPDATE_QUERY = 2;
+	private static final int DELETE_QUERY = 3;
+	
+	private int queryType;
+	private String table;
+	private Map<String, Object> data = new LinkedHashMap<>();
+	private String idKey;
+	
+	private SQLQueryBuilder(int queryType, String table) {
+		this.queryType = queryType;
+		this.table = table;
+	}
+	
+	public void add(String fieldName) {
+		add(fieldName, null);
+	}
+	
+	public void add(String fieldName, Object value) {
+		if (data.containsKey(fieldName)) {
+			data.replace(fieldName, value);
+		} else {
+			data.put(fieldName, value);
+		}
+	}
+	
+	public void addId(String idName, Object value) {
+		add(idName, value);
+		idKey = idName;
+	}
+	
+	public void remove(String fieldName) {
+		if (data.containsKey(fieldName)) {
+			data.remove(fieldName);
+		}
+	}
+	
+	public List<String> listField() {
+		List<String> result = new ArrayList<>();
+		data.forEach((key, val) -> result.add(key));
+		return result;
+	}
+	
+	public String getSQL() {
+		switch(queryType) {
+			case SELECT_QUERY:
+				return select();
+			case INSERT_QUERY:
+				return insert();
+			case UPDATE_QUERY:
+				return update();
+			case DELETE_QUERY:
+				return delete();
+			default:
+				return null;
+		}
+	}
+	
+	public String getSQL(String append) {
+		return getSQL() + append;
+	}
+	
+	public List<Object> getParams() {
+		List<Object> result = new ArrayList<>();
+		data.forEach((key, val) -> {
+			if (idKey != null) {
+				if (!idKey.equals(key)) {
+					result.add(val);
+				}
+			} else {
+				result.add(val);
+			}
+		});
+		if (idKey != null) {
+			result.add(data.get(idKey));
+		}
+		return result;
+	}
+	
+	public String toSQL() {
+		return getSQL();
+	}
+	
+	@Override
+	public String toString() {
+		return getSQL();
+	}
+	
+	public static SQLQueryBuilder selectQuery(String table) {
+		return new SQLQueryBuilder(SELECT_QUERY, table);
+	}
+	
+	public static SQLQueryBuilder insertQuery(String table) {
+		return new SQLQueryBuilder(INSERT_QUERY, table);
+	}
+	
+	public static SQLQueryBuilder updateQuery(String table) {
+		return new SQLQueryBuilder(UPDATE_QUERY, table);
+	}
+	
+	public static SQLQueryBuilder deleteQuery(String table) {
+		return new SQLQueryBuilder(DELETE_QUERY, table);
+	}
+	
+	private String select() {
+		StringBuilder sql = new StringBuilder();
+		sql.append("Select * From ");
+		sql.append(table);
+		sql.append(" Where 1=1");
+		data.forEach((key, val) -> {
+			sql.append(" And ");
+			sql.append(key);
+			sql.append(" = ?");
+		});
+		return sql.toString();
+	}
+	
+	private String insert() {
+		StringBuilder sql = new StringBuilder();
+		StringBuilder val = new StringBuilder();
+		boolean first = true;
+		sql.append("Insert into ");
+		sql.append(table);
+		sql.append("(");
+		for(Entry<String, Object> entry : data.entrySet()) {
+			if (!first) {
+				sql.append(",");
+				val.append(",");
+			}
+			sql.append(entry.getKey());
+			val.append("?");
+			first = false;
+		}
+		sql.append(") Values(");
+		sql.append(val);
+		sql.append(")");
+		return sql.toString();
+	}
+	
+	private String update() {
+		// Si pas d'id indiqué
+		if (idKey == null) {
+			return null;
+		}
+		StringBuilder sql = new StringBuilder();
+		boolean first = true;
+		sql.append("Update ");
+		sql.append(table);
+		sql.append(" Set ");
+		for(Entry<String, Object> entry : data.entrySet()) {
+			// On ajoute pas la clef
+			if(idKey.equals(entry.getKey())) {
+				continue;
+			}
+			if (!first) {
+				sql.append(",");
+			}
+			sql.append(entry.getKey());
+			sql.append(" = ?");
+			first = false;
+		}
+		sql.append(" Where ");
+		sql.append(idKey);
+		sql.append(" = ?");
+		return sql.toString();
+	}
+	
+	private String delete() {
+		// Si pas d'id indiqué
+		if (idKey == null) {
+			return null;
+		}
+		StringBuilder sql = new StringBuilder();
+		sql.append("Delete From ");
+		sql.append(table);
+		sql.append(" Where 1=1");
+		data.forEach((key, val) -> {
+			if (!idKey.equals(key)) {
+				sql.append(" And ");
+				sql.append(key);
+				sql.append(" = ?");
+			}
+		});
+		sql.append(" And ");
+		sql.append(idKey);
+		sql.append(" = ?");
+		return sql.toString();
+	}
+
+}

+ 10 - 16
src/db/mapper/DatabaseMapper.java

@@ -25,11 +25,12 @@ public class DatabaseMapper {
 						return null;
 					}
 					for(Field field : dbTable.entity().getFields()) {
-						if(!field.isAnnotationPresent(DbField.class)) {
+						String dbfield;
+						if((dbfield = DatabaseUtils.getDbField(field)) == null) {
 							continue;
 						}
 						// Recup valeur dans le resultat
-						Object value = rs.getObject(field.getAnnotation(DbField.class).value());
+						Object value = rs.getObject(dbfield);
 						// Si lien avec une autre entité
 						String link = DatabaseUtils.getDbLink(field);
 						if(value != null && link != null) {
@@ -38,18 +39,14 @@ public class DatabaseMapper {
 							if(otherId == null || otherId == 0) {
 								value = null;
 							} else {
-								String[] split = field.getType().getName().split("\\.");
-								String className = split[split.length - 1];
-								// Chargement de la class Table qui gère l'entité
-								Class<?> clazz = Class.forName(link + "." + className + "Table");
-								DatabaseTable table = (DatabaseTable) clazz.getMethod("getInstance").invoke(null);
+								DatabaseTable table = DatabaseUtils.getDatabaseTable(field);
 								value = table.getById(otherId);
 							}
 						}
 						field.set(obj, value);
 					}
 					return obj;
-				} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | SQLException | ClassNotFoundException e) {
+				} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | SQLException e) {
 					throw new IllegalStateException("Unable to map value to the entity", e);
 				}
 				
@@ -66,11 +63,12 @@ public class DatabaseMapper {
 					while(rs.next()) {
 						T obj = (T) dbTable.entity().getConstructor().newInstance();
 						for(Field field : dbTable.entity().getFields()) {
-							if(!field.isAnnotationPresent(DbField.class)) {
+							String dbfield;
+							if((dbfield = DatabaseUtils.getDbField(field)) == null) {
 								continue;
 							}
 							// Recup valeur dans le resultat
-							Object value = rs.getObject(field.getAnnotation(DbField.class).value());
+							Object value = rs.getObject(dbfield);
 							// Si lien avec une autre entité
 							String link = DatabaseUtils.getDbLink(field);
 							if(value != null && link != null) {
@@ -79,11 +77,7 @@ public class DatabaseMapper {
 								if(otherId == null || otherId == 0) {
 									value = null;
 								} else {
-									String[] split = field.getType().getName().split("\\.");
-									String className = split[split.length - 1];
-									// Chargement de la class Table qui gère l'entité
-									Class<?> clazz = Class.forName(link + "." + className + "Table");
-									DatabaseTable table = (DatabaseTable) clazz.getMethod("getInstance").invoke(null);
+									DatabaseTable table = DatabaseUtils.getDatabaseTable(field);
 									value = table.getById(otherId);
 								}
 							}
@@ -92,7 +86,7 @@ public class DatabaseMapper {
 						list.add(obj);
 					}
 					return list;
-				} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | SQLException | ClassNotFoundException e) {
+				} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | SQLException e) {
 					throw new IllegalStateException("Unable to map value to the entity");
 				}
 			}

+ 30 - 2
src/servlets/TestDb.java

@@ -42,7 +42,7 @@ public class TestDb extends HttpServlet {
 	/**
 	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 	 */
-	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
 		EcoleTable et = EcoleTable.getInstance();
 		List<Ecole> le = et.getAll();
 		System.out.println("---------------------");
@@ -63,7 +63,6 @@ public class TestDb extends HttpServlet {
 		System.out.println("---------------------");
 		System.out.println(v.libelle);
 		
-		
 		UsagerTable ut = UsagerTable.getInstance();
 		Usager u = ut.getById(1);
 		System.out.println("---------------------");
@@ -71,6 +70,35 @@ public class TestDb extends HttpServlet {
 		System.out.println(u.ville);
 		System.out.println(u.ecole);
 		
+		e = et.getById(3);
+		e.ville = null;
+		System.out.println("---------------------");
+		System.out.println(e);
+		et.save(e);
+		le = et.getAll();
+		le.forEach(elt -> System.out.println(elt.libelle + " " + elt.ville + " " + elt.niveau));
+		
+		e.ville = et.getById(2).ville;
+		et.save(e);
+		System.out.println("---------------------");
+		le = et.getAll();
+		le.forEach(elt -> System.out.println(elt));
+		
+		Ecole nou = new Ecole();
+		nou.libelle = "Pixel";
+		nou.ville = new Ville();
+		nou.ville.libelle = "SPAAAACE";
+		nou.ville.cp = "12345";
+		et.save(nou);
+		System.out.println("---------------------");
+		le = et.getAll();
+		le.forEach(elt -> System.out.println(elt));
+		
+		et.del(e);
+		System.out.println("---------------------");
+		le = et.getAll();
+		le.forEach(elt -> System.out.println(elt));
+		
 		response.getWriter().append("Served at: ").append(request.getContextPath());
 	}