Skip to content

Instantly share code, notes, and snippets.

@emanoelqueiroz
Created July 13, 2017 19:01
Show Gist options
  • Save emanoelqueiroz/9676683b7d9c118a5aa0c1b9befc0f7e to your computer and use it in GitHub Desktop.
Save emanoelqueiroz/9676683b7d9c118a5aa0c1b9befc0f7e to your computer and use it in GitHub Desktop.
private void setParentId(T parentEntity, List<T> listEntity) {
try {
Long parentId = null;
for (Field field : parentEntity.getClass().getDeclaredFields()) {
if (field.getDeclaredAnnotation(SequenceId.class) == null) {
continue;
}
PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(parentEntity.getClass(), field.getName());
parentId = (Long) pd.getReadMethod().invoke(parentEntity);
break;
}
if (parentId == null) {
return;
}
for (T entity : listEntity) {
for (Field field : entity.getClass().getDeclaredFields()) {
if (field.getDeclaredAnnotation(ParentId.class) == null) {
continue;
}
PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(entity.getClass(), field.getName());
pd.getWriteMethod().invoke(entity, parentId);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment