I have a query something like this where I am trying to select from a view and then persist the view result as new entities of a certain class.
e.g. create new objects of MyEntity.class somewhat like follows.
I have named the view’s columns identical to the entity’s columns in the database:
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
List<MyEntity> myEntityList = em.createNativeQuery("select * from some_view", MyEntity.class)
.getResultList();
myEntityList.forEach(myEntity-> {
em.persist(myEntity);
});
tx.commit;
}
But I keep getting duplicate key violation even though the MyEntity objects do not actually exist in the MyEntity table in the database.
Wondering a better method of doing something like this if possible without actually having to convert the nativeQueries Object[] list to new MyEntity objects. I am trying to process thousands of entities and want it as quick as possible.
Note: the MyEntity class is a BaseGenericIdEntity<MyEntityId>
type class
Is this possible?