How to get the JPA generated SQL query?

前端 未结 6 1219
北荒
北荒 2020-12-28 17:50

I use JPA specification and Hibernate as my vendor. I need somehow to take the generated SQL Query which is sent to the the DB (printed to the sysout) and save it as a simpl

6条回答
  •  醉酒成梦
    2020-12-28 18:12

    If I understand you correctly, you want to get the insert query which Hibernate is executed on one database, and via code, run it on a different database via entityManager#executeUpdate or similar.

    Hibernate does not expose the generated query as it is specific for the dialect of target database. So even if were to get the insert query, it could be pointless.

    However in your case, you can create two database connections (via two DataSource or EntityManagerFactory whatever in your case) and call dao.persist(entity) two times for both databases, and let Hibernate handle the query construction part.

    Edit: By query I mean native query here, HQL query would be same for both databases. Hope it helps.

提交回复
热议问题