Skip to content

Commit 0679607

Browse files
Fix NPE on adding new columns in the tables
1 parent a4b1a27 commit 0679607

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

framework/db/src/main/java/com/cloud/utils/db/GenericDaoBase.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import net.sf.ehcache.Cache;
9090
import net.sf.ehcache.CacheManager;
9191
import net.sf.ehcache.Element;
92+
import org.springframework.util.ClassUtils;
9293

9394
/**
9495
* GenericDaoBase is a simple way to implement DAOs. It DOES NOT
@@ -2047,16 +2048,23 @@ public boolean unremove(ID id) {
20472048

20482049
@DB()
20492050
protected void setField(final Object entity, final ResultSet rs, ResultSetMetaData meta, final int index) throws SQLException {
2050-
Attribute attr = _allColumns.get(new Pair<String, String>(meta.getTableName(index), meta.getColumnName(index)));
2051+
String tableName = meta.getTableName(index);
2052+
String columnName = meta.getColumnName(index);
2053+
Attribute attr = _allColumns.get(new Pair<>(tableName, columnName));
20512054
if (attr == null) {
20522055
// work around for mysql bug to return original table name instead of view name in db view case
20532056
Table tbl = entity.getClass().getSuperclass().getAnnotation(Table.class);
20542057
if (tbl != null) {
2055-
attr = _allColumns.get(new Pair<String, String>(tbl.name(), meta.getColumnLabel(index)));
2058+
attr = _allColumns.get(new Pair<>(tbl.name(), meta.getColumnLabel(index)));
20562059
}
20572060
}
2058-
assert (attr != null) : "How come I can't find " + meta.getCatalogName(index) + "." + meta.getColumnName(index);
2059-
setField(entity, attr.field, rs, index);
2061+
assert (attr != null) : "How come I can't find " + tableName + "." + columnName;
2062+
if(attr == null) {
2063+
logger.warn(String.format("Failed to find attribute in the entity %s to map column %s.%s (%s)",
2064+
ClassUtils.getUserClass(entity).getSimpleName(), tableName, columnName));
2065+
} else {
2066+
setField(entity, attr.field, rs, index);
2067+
}
20602068
}
20612069

20622070
@Override

0 commit comments

Comments
 (0)