Browse Source

Gestion absence cache et objet null par le mapper

Arthur Brandao 5 years ago
parent
commit
2842d0d229
3 changed files with 13 additions and 5 deletions
  1. 1 1
      src/database.properties
  2. 6 0
      src/db/Database.java
  3. 6 4
      src/db/mapper/DatabaseMapper.java

+ 1 - 1
src/database.properties

@@ -7,5 +7,5 @@ manager=microfolie.db.MicrofolieDatabase
 # Valeur par défaut de @DbLink
 dblink=microfolie.db.table
 
-# Active ou non le cache des données récupèré en base /!\ Ne desactiver le cache que pour du debug, provoque des dysfonctionnement
+# Active ou non le cache des données récupèré en base
 cache=false

+ 6 - 0
src/db/Database.java

@@ -97,6 +97,9 @@ 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);
 		} catch (SQLException | IllegalStateException e) {
 			LOGGER.warning(e.getMessage());
@@ -112,6 +115,9 @@ public class Database {
 			}
 			ResultSet result = st.executeQuery();
 			T obj = mapper.map(result);
+			if (obj == null) {
+				return Optional.empty();
+			}
 			return Optional.of(obj);
 		} catch (SQLException | IllegalStateException e) {
 			LOGGER.warning(e.getMessage());

+ 6 - 4
src/db/mapper/DatabaseMapper.java

@@ -21,7 +21,9 @@ public class DatabaseMapper {
 			public T map(ResultSet rs) {
 				try {
 					T obj = (T) dbTable.entity().getConstructor().newInstance();
-					rs.next();
+					if (!rs.next()) {
+						return null;
+					}
 					for(Field field : dbTable.entity().getFields()) {
 						if(!field.isAnnotationPresent(DbField.class)) {
 							continue;
@@ -33,7 +35,7 @@ public class DatabaseMapper {
 						if(value != null && link != null) {
 							DatabaseUtils.checkIfPersistable(field);
 							Long otherId = (Long) value;
-							if(otherId == 0) {
+							if(otherId == null || otherId == 0) {
 								value = null;
 							} else {
 								String[] split = field.getType().getName().split("\\.");
@@ -48,7 +50,7 @@ public class DatabaseMapper {
 					}
 					return obj;
 				} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | SQLException | ClassNotFoundException e) {
-					throw new IllegalStateException("Unable to map value to the entity");
+					throw new IllegalStateException("Unable to map value to the entity", e);
 				}
 				
 			}
@@ -74,7 +76,7 @@ public class DatabaseMapper {
 							if(value != null && link != null) {
 								DatabaseUtils.checkIfPersistable(field);
 								Long otherId = (Long) value;
-								if(otherId == 0) {
+								if(otherId == null || otherId == 0) {
 									value = null;
 								} else {
 									String[] split = field.getType().getName().split("\\.");