You can use Jackson's ObjectMapper library to achieve this. In order for this to work you must have getters declared in both EntityA and EntityB class, as well as a default (empty) constructor.
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Function converts list of objects to another list of objects
* which' type is specified by the clazz attribute. Clazz attribute
* must not be null, or it will throw a NullPointerException.
*
* @param list List of target objects
* @param clazz Class to map list objects to
* @param <T> Target class
* @param <F> From class
* @return F.class List converted to T.class List
*/
public static <T, F> List<T> convertList(List<F> list, Class<T> clazz) {
Objects.requireNonNull(clazz);
ObjectMapper mapper = new ObjectMapper();
// Important: this must be declared so mapper doesn't throw
// an exception for all properties which it can't map.
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return Optional.ofNullable(list)
.orElse(Collections.emptyList())
.stream()
.map(obj -> mapper.convertValue(obj, clazz))
.collect(Collectors.toList());
}
public void insertListItems(List<EntityA> entityAList) {
List<EntityB> entityBList = convertList(entityAList, EntityB.class);
// Continue with table insert operation
}
Answer from BrunoT on Stack OverflowYou can use Jackson's ObjectMapper library to achieve this. In order for this to work you must have getters declared in both EntityA and EntityB class, as well as a default (empty) constructor.
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Function converts list of objects to another list of objects
* which' type is specified by the clazz attribute. Clazz attribute
* must not be null, or it will throw a NullPointerException.
*
* @param list List of target objects
* @param clazz Class to map list objects to
* @param <T> Target class
* @param <F> From class
* @return F.class List converted to T.class List
*/
public static <T, F> List<T> convertList(List<F> list, Class<T> clazz) {
Objects.requireNonNull(clazz);
ObjectMapper mapper = new ObjectMapper();
// Important: this must be declared so mapper doesn't throw
// an exception for all properties which it can't map.
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return Optional.ofNullable(list)
.orElse(Collections.emptyList())
.stream()
.map(obj -> mapper.convertValue(obj, clazz))
.collect(Collectors.toList());
}
public void insertListItems(List<EntityA> entityAList) {
List<EntityB> entityBList = convertList(entityAList, EntityB.class);
// Continue with table insert operation
}
You could try to get a Map like this Map<String, List<String> columns;
The first value (key) is the column name and the second value (value) is a list of all values of this column.
Then you can just use if statements for selecting a column or not.
So:
- Load all columns and their values
- Use a loop with if statements.
- In this loop, you could also integrate a list of Strings, which are the unallowed or allowed column names.
There are some libraries around there:
Commons-BeanUtils: ConvertUtils -> Utility methods for converting String scalar values to objects of the specified Class, String arrays to arrays of the specified Class.
Commons-Lang: ArrayUtils -> Operations on arrays, primitive arrays (like
int[]) and primitive wrapper arrays (likeInteger[]).Spring framework: Spring has an excellent support for PropertyEditors, that can also be used to transform Objects to/from Strings.
Dozer: Dozer is a powerful, yet simple Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.
ModelMapper: ModelMapper is an intelligent object mapping framework that automatically maps objects to each other. It uses a convention based approach to map objects while providing a simple refactoring safe API for handling specific use cases.
MapStruct: MapStruct is a compile-time code generator for bean mappings, resulting in fast (no usage of reflection or similar), dependency-less and type-safe mapping code at runtime.
Orika: Orika uses byte code generation to create fast mappers with minimal overhead.
Selma: Compile-time code-generator for mappings
JMapper: Bean mapper generation using Annotation, XML or API
- seems dead, last update in 2023
Smooks: The Smooks JavaBean Cartridge allows you to create and populate Java objects from your message data (i.e. bind data to) (suggested by superfilin in comments).
- No longer under active development
Commons-Convert: Commons-Convert aims to provide a single library dedicated to the task of converting an object of one type to another. The first stage will focus on Object to String and String to Object conversions.
- seems dead, last update in 2010
Transmorph: Transmorph is a free java library used to convert a Java object of one type into an object of another type (with another signature, possibly parameterized).
- seems dead, last update in 2013
EZMorph: EZMorph is simple java library for transforming an Object to another Object. It supports transformations for primitives and Objects, for multidimensional arrays and transformations with DynaBeans
- seems dead, last update in 2008
Morph: Morph is a Java framework that eases the internal interoperability of an application. As information flows through an application, it undergoes multiple transformations. Morph provides a standard way to implement these transformations.
- seems dead, last update in 2008
Lorentz: Lorentz is a generic object-to-object conversion framework. It provides a simple API to convert a Java objects of one type into an object of another type.
- seems dead
OTOM: With OTOM, you can copy any data from any object to any other object. The possibilities are endless. Welcome to "Autumn".
- seems dead
You could try Dozer.
Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. Typically, these Java Beans will be of different complex types.
Dozer supports simple property mapping, complex type mapping, bi-directional mapping, implicit-explicit mapping, as well as recursive mapping. This includes mapping collection attributes that also need mapping at the element level.