I am creating a class to get a record of a database and write to another database.
I'm using JPA
and I ran into a problem. I'm doing a generic insert and I have to wipe the ID
of the table to be able to insert, as it arrives at me a Object
I do not know which field to clean and nor how ...
I need two things then, to identify which class property is @Id
and how to clean it.
In the code below I put a comment aqui
showing where the unknown is.
class InsertObject implements Runnable {
private final int integrationId;
private final Class entityClass;
private final EntityManager emOrigem;
private final EntityManager emDestino;
public InsertObject(int integrationId, Class entityClass, EntityManager emOrigem, EntityManager emDestino) {
this.integrationId = integrationId;
this.entityClass = entityClass;
this.emOrigem = emOrigem;
this.emDestino = emDestino;
}
@Override
public void run() {
// carrega objeto origem
String sql = "SELECT x FROM " + entityClass.getSimpleName() + " x WHERE integracaoId = " + integrationId;
Query qOrigem = emOrigem.createQuery(sql);
Object oOrigem = qOrigem.getSingleResult();
// remove id
emOrigem.detach(oOrigem);
//oOrigem.setId(null); // <<<< AQUI <<<<<<<<<
// salva no destino
emDestino.getTransaction().begin();
emDestino.persist(oOrigem);
emDestino.getTransaction().commit();
}
}