Skip to content

Commit

Permalink
Refactored BaseMapper and fixed bug that occurred when using foreign …
Browse files Browse the repository at this point in the history
…key objects.
  • Loading branch information
CollinAlpert committed Sep 22, 2018
1 parent b36f119 commit ee65f49
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ First, include the Maven artifact:
<dependency>
<groupId>com.github.collinalpert</groupId>
<artifactId>java2db</artifactId>
<version>1.5.2</version>
<version>1.5.3</version>
</dependency>
```
Or include the [JAR](https://github.com/CollinAlpert/Java2DB/releases/) in your project. To begin using this library, you need to do two things on program start (or whenever you feel like it, it just may not work then):
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.collinalpert</groupId>
<artifactId>java2db</artifactId>
<version>1.5.2</version>
<version>1.5.3</version>
<packaging>jar</packaging>

<name>Java2DB</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* @author Collin Alpert
Expand Down Expand Up @@ -60,10 +59,10 @@ public Optional<T> map(ResultSet set) {
* @return A list of Java entities.
*/
public List<T> mapToList(ResultSet set) {
List<T> list = new ArrayList<>();
var list = new ArrayList<T>();
try {
while (set.next()) {
T entity = IoC.resolve(clazz);
var entity = IoC.resolve(clazz);
setFields(set, entity);
list.add(entity);
}
Expand All @@ -86,33 +85,35 @@ private void setFields(ResultSet set, T entity) throws IllegalAccessException {
var fields = Utilities.getAllFields(entity, true);
try {
var foreignKeys = getForeignKeys(fields, set);
var foreignKeyObjects = getForeignKeyObjects(fields);
for (var field : fields) {
field.setAccessible(true);
if (field.getAnnotation(ForeignKeyObject.class) != null) {
ForeignKeyObject fkObjectNumber = field.getAnnotation(ForeignKeyObject.class);
Class<?> clz = foreignKeyObjects.keySet().stream().filter(x -> x == field.getType()).findFirst().orElseThrow();
var service = IoC.resolveServiceByEntity((Class<? extends BaseEntity>) clz);
var fkObject = service.getById(foreignKeys.get(fkObjectNumber.value()));
if (fkObject.isPresent()) {
field.set(entity, fkObject.get());
} else {
ForeignKeyObject foreignKeyObject;
if ((foreignKeyObject = field.getAnnotation(ForeignKeyObject.class)) != null) {
if (!BaseEntity.class.isAssignableFrom(field.getType())) {
throw new IllegalArgumentException(String.format("Foreign key object %s with id %d does not extend BaseEntity.", field.getType(), foreignKeyObject.value()));
}
var service = IoC.resolveServiceByEntity((Class<? extends BaseEntity>) field.getType());
var optionalEntity = service.getById(foreignKeys.get(foreignKeyObject.value()));
if (!optionalEntity.isPresent()) {
System.err.printf("Could not set type %s with name %s\n", field.getType(), field.getName());
continue;
}
field.set(entity, optionalEntity.get());
continue;
}
var value = set.getObject(field.getName(), field.getType());
field.set(entity, value);

}
} catch (SQLException e) {
e.printStackTrace();
//TODO add possibility of custom mapping classes.
throw new IllegalArgumentException("Cannot use base mapping. Please define custom mapping in according mapping class.");
}
}


/**
* @param fields List of fields to get the foreign key annotations from.
* @param fields Map of fields to get the foreign key annotations from.
* @param set The {@link ResultSet} is needed to get the actual foreign key.
* @return A {@link Map} where the keys are the id number of this foreign key and the values are the actual foreign keys.
* @throws SQLException when the foreign key in the {@link ResultSet} does not exist.
Expand All @@ -127,15 +128,4 @@ private Map<Integer, Integer> getForeignKeys(ArrayList<Field> fields, ResultSet
}
return map;
}

/**
* Gets the classes of all fields that represent a foreign key object, i.e. do not exist on the database.
*
* @param fields A list of fields to get the foreign key objects from.
* @return A {@link Map} where the keys are the classes of the foreign keys and the value is the id number for this foreign key.
*/
private Map<Class<?>, Integer> getForeignKeyObjects(ArrayList<Field> fields) {
return fields.stream().filter(field -> field.getAnnotation(ForeignKeyObject.class) != null)
.collect(Collectors.toMap(Field::getType, field -> field.getAnnotation(ForeignKeyObject.class).value()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public BaseService(Class<T> clazz) {
* Creates this Java entity on the database.
*
* @param instance The instance to create on the database.
* @throws SQLException if the query cannot be executed due to database constraints
* i.e. non-existing default value for field or an incorrect data type.
*/
public void create(T instance) throws SQLException {
var insertQuery = new StringBuilder("insert into `").append(tableName).append("` (");
Expand Down Expand Up @@ -180,6 +182,8 @@ public List<T> getAll() {
* Updates this entity's row on the database.
*
* @param instance The instance to update on the database.
* @throws SQLException if the query cannot be executed due to database constraints
* i.e. non-existing default value for field or an incorrect data type.
*/
public void update(T instance) throws SQLException {
var updateQuery = new StringBuilder("update `").append(tableName).append("` set ");
Expand Down

0 comments on commit ee65f49

Please sign in to comment.