2
I’m creating a class to get a database record and write to another database.
I’m using JPA
and bumped into a problem. I’m doing a generic insertion and I have to clean up the ID
of the table to be able to insert, as enough for me a Object
I don’t know which field to clear and how...
I need two things then, 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();
}
}