/** * No checking on the annotations - client could get unpredictable behaviour if annotations aren't * configured properly. * * @return */ public void discover() { jpaEntity = entityClass.isAnnotationPresent(javax.persistence.Entity.class); Table table = (Table) entityClass.getAnnotation(Table.class); if (!jpaEntity || table == null) throw new UnsupportedOperationException(); // throw a more appropriate exception tableName = table.name(); // Absolutely no validation annotation parsing!! // this will ALWAYS parse annotations in this order: ID, EmbeddedId, and Column Field[] fields = entityClass.getDeclaredFields(); for (Field f : fields) { // first Id annotation Annotation fan = f.getAnnotation(Id.class); if (fan != null) { Column col = (Column) f.getAnnotation(Column.class); idMapping = new ColumnMapping(col.name(), f); continue; } // Embedded ID fan = f.getAnnotation(EmbeddedId.class); if (fan != null) { ArrayList<ColumnMapping> emList = new ArrayList<>(); for (Field inF : f.getType().getDeclaredFields()) { if (inF.getAnnotation(Column.class) != null) { Column innerId = (Column) inF.getAnnotation(Column.class); // put column name in lower case as driver does emList.add(new ColumnMapping(innerId.name().toLowerCase(), inF)); } } // column name embedded = new Embedded(f, emList.toArray(emptyColArr)); continue; } // finally get the column // there's special handling for map type - which is a definite area for improvement fan = f.getAnnotation(Column.class); if (fan != null) { Column col = (Column) fan; ColumnMapping mapping = new ColumnMapping(col.name(), f); // special consideration for map if (Map.class.isAssignableFrom(f.getType())) { mapping.isMap = true; } // put in lower case as the driver colsToFields.put(col.name().toLowerCase(), mapping); continue; } } }
/** * Might move this out to a utility class This has been moved to {@link * AbstractEntityManager#getUpdateStatement(Object)} * * @param obj * @return */ @Deprecated public SimpleStatement getUpdateStatement(T obj) { StringBuilder builder = new StringBuilder("UPDATE ").append(tableName).append(" SET "); ArrayList<Object> valueList = new ArrayList<>(); int i = 0; // go through the columns for (String col : colsToFields.keySet()) { ColumnMapping mapping = colsToFields.get(col); Object valueObj = mapping.get(obj); if (!mapping.isMap) { builder.append(getColUpdate(col)); valueList.add(valueObj); } else { Map<String, String> map = (Map<String, String>) valueObj; if (map != null) { int im = 0; for (String key : map.keySet()) { builder.append(col).append(openbsq).append(key).append(sqcloseb).append(eqParam); if (im++ < map.size() - 1) builder.append(comma).append(space); valueList.add(map.get(key)); } } } if (i++ < (colsToFields.size() - 1)) builder.append(comma).append(space); } builder.append(getIdPredicate()); if (embedded != null) { try { // this needs some improvement to the embedded class Object idObj = embedded.field.get(obj); for (ColumnMapping membed : embedded.columns) { valueList.add(membed.get(idObj)); } } catch (Exception e) { e.printStackTrace(); } } SimpleStatement ss = new SimpleStatement(builder.toString(), valueList.toArray()); // System.out.println("update ss : " + ss.getQueryString()); return ss; }
/** * Populates and iterates through a {@link Row} using {@link ColumnDefinitions} provided by the * driver. * * @param row * @return */ public T get(Row row) { // Entity and its associated id object T entity = null; Object idObj = null; try { entity = entityClass.newInstance(); idObj = null; ColumnMapping idcolmap = null; // Note that this early invocation means that // you can't populate on #setId in your java bean method if (embedded != null) { idObj = embedded.field.getType().newInstance(); embedded.set(entity, idObj); } else { idObj = idMapping.field.getType().newInstance(); idMapping.set(entity, idObj); } } catch (InstantiationException | IllegalAccessException e) { throw new IllegalAccessError( "A configuration exception has occurred in creating entity: " + entityClass); } ColumnDefinitions metaData = row.getColumnDefinitions(); List<Definition> defList = metaData.asList(); for (Definition def : defList) { ColumnMapping mapping = colsToFields.get(def.getName()); if (mapping == null) { // it could be an id column if (idMapping != null && def.getName().equals(idMapping.name)) { idMapping.set(entity, getValue(row, idMapping, def)); continue; } // else: need to find it, possible point of refactor ColumnMapping nembed = null; if (embedded != null) nembed = embedded.get(def.getName()); if (nembed != null) nembed.set(idObj, getValue(row, nembed, def)); continue; } Object value = getValue(row, mapping, def); // set it if (value != null) mapping.set(entity, value); } return entity; }