Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 78 additions & 4 deletions src/main/net/sf/persist/AnnotationTableMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;

import net.sf.persist.writer.Writer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -91,6 +95,7 @@ public AnnotationTableMapping(final Class<?> objectClass) {
if (annotation != null && annotation.autoGenerated()) {
autoGeneratedColumnsTemp.add(columnName);
}

}

if (primaryKeysList.isEmpty()) {
Expand Down Expand Up @@ -194,16 +199,40 @@ public Map<String, Method> getSettersMap() {
return settersMap;
}

public Optional<String> getFieldName(final String columnName) {
Optional<String> fieldName = Optional.ofNullable(columnsMap.get(columnName.toLowerCase(Locale.ENGLISH)));
if (!fieldName.isPresent()) {
ENGINE_LOG.warn("Column name [" + columnName + "] has no corresponding field.");
}
return fieldName;
}

@Override
public Method getGetterForColumn(final String columnName) {
final String fieldName = columnsMap.get(columnName.toLowerCase(Locale.ENGLISH));
return gettersMap.get(fieldName);
final Optional<String> fieldName = getFieldName(columnName);
if (fieldName.isPresent()) {
final Optional<Method> getterForFieldName = Optional.ofNullable(gettersMap.get(fieldName.get()));
if (!getterForFieldName.isPresent()) {
throw new NoSuchElementException(
"Could not find getter for columnn with field name [" + fieldName + "]");
}
return getterForFieldName.get();
}
return null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG.warn - column exists in table that does not have a corresponding getter (or setter below) in the value class, ignoring column.

}

@Override
public Method getSetterForColumn(final String columnName) {
final String fieldName = columnsMap.get(columnName.toLowerCase(Locale.ENGLISH));
return settersMap.get(fieldName);
final Optional<String> fieldName = getFieldName(columnName);
if (fieldName.isPresent()) {
final Optional<Method> setterForFieldName = Optional.ofNullable(settersMap.get(fieldName.get()));
if (!setterForFieldName.isPresent()) {
throw new NoSuchElementException(
"Could not find setter for columnn with field name [" + fieldName + "]");
}
return setterForFieldName.get();
}
return null;
}

public String getSelectSql() {
Expand All @@ -226,6 +255,51 @@ public String getDeleteSql() {
return deleteSql;
}

@Override
public Class<?> getOptionalSubType(final String columnName) {
final Optional<String> fieldName = getFieldName(columnName);
if (fieldName.isPresent()) {
final Optional<Class<?>> optSubTypeForFieldName =
Optional.ofNullable(annotationsMap.get(fieldName.get()).optionalSubType());
if (!optSubTypeForFieldName.isPresent()) {
throw new NoSuchElementException(
"Could not find optional subtype for columnn with field name [" + fieldName + "]");
}
return optSubTypeForFieldName.get();
}
return null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use case?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if field name is not defined, we don't care, so return a null

}

@Override
public Class<?> getSerializationType(final String columnName) {
final Optional<String> fieldName = getFieldName(columnName);
if (fieldName.isPresent()) {
final Optional<Class<?>> serializationTypeForFieldName =
Optional.ofNullable(annotationsMap.get(fieldName.get()).serializeAs());
if (!serializationTypeForFieldName.isPresent()) {
throw new NoSuchElementException(
"Could not find serialization type for columnn with field name [" + fieldName + "]");
}
return serializationTypeForFieldName.get();
}
return null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use case?

}

@Override
public Class<? extends Writer> getWriterClass(final String columnName) {
final Optional<String> fieldName = getFieldName(columnName);
if (fieldName.isPresent()) {
final Optional<Class<? extends Writer>> writerClassForFieldName =
Optional.ofNullable(annotationsMap.get(fieldName.get()).writerClass());
if (!writerClassForFieldName.isPresent()) {
throw new NoSuchElementException(
"Could not find writer class for columnn with field name [" + fieldName + "]");
Copy link

@kwburgess kwburgess Jul 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

columnn -> column (for this line and above exceptions)

}
return writerClassForFieldName.get();
}
return null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use case?

}

// ---------- helpers ----------

private static String[] toArray(final List<String> list) {
Expand Down
17 changes: 13 additions & 4 deletions src/main/net/sf/persist/Mapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@
import java.util.HashMap;
import java.util.Map;

import net.sf.persist.writer.Writer;

public abstract class Mapping {

public abstract Method getGetterForColumn(String columnName);

public abstract Method getSetterForColumn(String columnName);

public abstract Class<?> getOptionalSubType(String columnName);

public abstract Class<?> getSerializationType(String columnName);

public abstract Class<? extends Writer> getWriterClass(String columnName);

// ---------- utility methods ----------

/**
Expand Down Expand Up @@ -49,7 +57,7 @@ protected static Map[] getFieldsMaps(final Class<?> objectClass) {

// create map with all getters and setters

final Map<String, Method[]> allMethods = new HashMap<String, Method[]>();
final Map<String, Method[]> allMethods = new HashMap<>();
for (Method method : methods) {
final String name = method.getName();

Expand Down Expand Up @@ -86,9 +94,10 @@ protected static Map[] getFieldsMaps(final Class<?> objectClass) {
// a field is only taken into consideration if it has a getter and a
// setter

final Map<String, net.sf.persist.annotations.Column> annotationsMap = new HashMap<String, net.sf.persist.annotations.Column>();
final Map<String, Method> gettersMap = new HashMap<String, Method>();
final Map<String, Method> settersMap = new HashMap<String, Method>();
final Map<String, net.sf.persist.annotations.Column> annotationsMap = new HashMap<>();
final Map<String, Method> gettersMap = new HashMap<>();
final Map<String, Method> settersMap = new HashMap<>();
final Map<String, Class<?>> optionalSubTypeMap = new HashMap<>();

for (String suffix : allMethods.keySet()) {

Expand Down
102 changes: 85 additions & 17 deletions src/main/net/sf/persist/NoTableMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;

import net.sf.persist.writer.Writer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Represents the mapping of columns to getters and setters of a POJO.
* <p>
Expand All @@ -17,6 +25,8 @@
*/
public class NoTableMapping extends Mapping {

private static final Logger ENGINE_LOG = LoggerFactory.getLogger("persist.engine");

// POJO class
private final Class objectClass;

Expand All @@ -29,6 +39,8 @@ public class NoTableMapping extends Mapping {
// map possible column names to field names
private final Map<String, String> columnsMap;

private final Map<String, net.sf.persist.annotations.Column> annotationsMap;

public NoTableMapping(Class objectClass, NameGuesser nameGuesser) {

checkAnnotation(objectClass);
Expand All @@ -37,7 +49,7 @@ public NoTableMapping(Class objectClass, NameGuesser nameGuesser) {

// get the list of annotations, getters and setters
Map[] fieldsMaps = Mapping.getFieldsMaps(objectClass);
final Map<String, net.sf.persist.annotations.Column> annotationsMap = fieldsMaps[0];
annotationsMap = fieldsMaps[0];
gettersMap = fieldsMaps[1];
settersMap = fieldsMaps[2];

Expand Down Expand Up @@ -91,16 +103,10 @@ public NoTableMapping(Class objectClass, NameGuesser nameGuesser) {

}

/**
* Returns the field name associated with a given column. If a mapping can't
* be found, will throw a PersistException.
*/
public String getFieldNameForColumn(String columnName) {
String fieldName = columnsMap.get(columnName);
if (fieldName == null) {
throw new PersistException("Could map field for column [" + columnName + "] on class ["
+ objectClass.getCanonicalName()
+ "]. Please specify an explict @Column annotation for that column.");
public Optional<String> getFieldName(final String columnName) {
Optional<String> fieldName = Optional.ofNullable(columnsMap.get(columnName.toLowerCase(Locale.ENGLISH)));
if (!fieldName.isPresent()) {
ENGINE_LOG.warn("Column name [" + columnName + "] has no corresponding field.");
}
return fieldName;
}
Expand All @@ -112,9 +118,17 @@ public String getFieldNameForColumn(String columnName) {
* @see Mapping
*/
@Override
public Method getSetterForColumn(String columnName) {
String fieldName = getFieldNameForColumn(columnName);
return settersMap.get(fieldName);
public Method getSetterForColumn(final String columnName) {
final Optional<String> fieldName = getFieldName(columnName);
if (fieldName.isPresent()) {
final Optional<Method> setterForFieldName = Optional.ofNullable(settersMap.get(fieldName.get()));
if (!setterForFieldName.isPresent()) {
throw new NoSuchElementException(
"Could not find setter for columnn with field name [" + fieldName + "]");
}
return setterForFieldName.get();
}
return null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add warn (similar to getter)

}

/**
Expand All @@ -124,11 +138,65 @@ public Method getSetterForColumn(String columnName) {
* @see Mapping
*/
@Override
public Method getGetterForColumn(String columnName) {
String fieldName = getFieldNameForColumn(columnName);
return gettersMap.get(fieldName);
public Method getGetterForColumn(final String columnName) {
final Optional<String> fieldName = getFieldName(columnName);
if (fieldName.isPresent()) {
final Optional<Method> getterForFieldName = Optional.ofNullable(gettersMap.get(fieldName.get()));
if (!getterForFieldName.isPresent()) {
throw new NoSuchElementException(
"Could not find getter for columnn with field name [" + fieldName + "]");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comments as above (would love to understand the difference between "No Table Mapping" and "annotation table mapping " in persist library)

}
return getterForFieldName.get();
}
return null;
}

@Override
public Class<?> getOptionalSubType(final String columnName) {
final Optional<String> fieldName = getFieldName(columnName);
if (fieldName.isPresent()) {
final Optional<Class<?>> optSubTypeForFieldName =
Optional.ofNullable(annotationsMap.get(fieldName.get()).optionalSubType());
if (!optSubTypeForFieldName.isPresent()) {
throw new NoSuchElementException(
"Could not find optional subtype for columnn with field name [" + fieldName + "]");
}
return optSubTypeForFieldName.get();
}
return null;
}

@Override
public Class<?> getSerializationType(final String columnName) {
final Optional<String> fieldName = getFieldName(columnName);
if (fieldName.isPresent()) {
final Optional<Class<?>> serializationTypeForFieldName =
Optional.ofNullable(annotationsMap.get(fieldName.get()).serializeAs());
if (!serializationTypeForFieldName.isPresent()) {
throw new NoSuchElementException(
"Could not find serialization type for columnn with field name [" + fieldName + "]");
}
return serializationTypeForFieldName.get();
}
return null;
}

@Override
public Class<? extends Writer> getWriterClass(final String columnName) {
final Optional<String> fieldName = getFieldName(columnName);
if (fieldName.isPresent()) {
final Optional<Class<? extends Writer>> writerClassForFieldName =
Optional.ofNullable(annotationsMap.get(fieldName.get()).writerClass());
if (!writerClassForFieldName.isPresent()) {
throw new NoSuchElementException(
"Could not find writer class for columnn with field name [" + fieldName + "]");
}
return writerClassForFieldName.get();
}
return null;
}


/**
* Checks if a given column name conflicts with an existing name in the
* columns map.
Expand Down
Loading