jdbctemplate query() vs entityManager createQuery() - dao

What is essential difference between these methods?
query() of JdbcTemplate and createQuery() of EntityManager?
As I understand, both execute query?

JdbcTemplate.query() executes a raw SQL query via Spring's JDBC API
EntityManager.createQuery() creates, but does not execute, a JPA query, via the native JPA API.
Same end result, very different mechanism.

Related

Spring-boot-jdbcTemplate Unit-testing

I am new to Spring and only somewhat experienced with JUnit I have the following method which requires a unit test
#Override
public List<Map<String, Object>> executeSqlQuery(String sql) {
List<Map<String, Object>> downloadRequest = jdbcTemplate.queryForList(sql);
return downloadRequest;
}
Does anyone have any suggestions on how I might achieve this using JUni?
Thank you very much in advance!
You can use these ways to unit test the function
Mocking the JDBC class :
The JDBCTemplate is a dependency for our class. Since in the unit test we moock the dependency, we could unit test this function by mocking the jdbcTemplate class.
Using an in-memory database :
If we are mocking the JDBC itself, you will see that we are not achieving much from the unit test.
One other practice which is followed is to use an in-memory database, you can use H2 Database. In this case, we will create the actual records in an in-memory database and then the query will be run over this fake database. This way we can insert multiple records in the database and check that our query is working fine. If you are using Spring, you can simply add #JdbcTest annotation on the test class to use H2 Database.
Reference: https://www.baeldung.com/spring-jdbctemplate-testing

Comparing JDBCTemplate and Hibernate for data access using spring

I have used both jdbcTemplate and Hibernate, but I am having a hard time comparing jdbcTemplate and hibernate in the context of performance, query execution time and batch updates.
My View and research did so far.
As hibernate uses HQL then its query is first translated into native language query and after that query is executed. So query execution time is high in case of hibernate(that's a different case as hibernate also support native query).
As Hibernate is ORM tool so we don't have to create the DataBase schema.
The batch insert of records is not piece of cake in hibernate as if the generation type of PK is Identity then batch insert won't be performed.
Is there are any other point when comparing JDBCTemplate and hibernate and when to use what?

Does using two JdbcTemplate objects with same DataSource, created within a #Transactional cause transaction issues?

I am using Spring Framework 4.x.x. I have a DAO object that makes use of #Transactional annotation on a method. Within this method, a new JdbcTemplate is created and some queries are run. Further, within the same method, another JdbcTemplate is created and more queries are run. Both JdbcTemplates use the same DataSource.
Is the entire method still a “Transaction?” Or does the second JdbcTemplate start it’s own transaction? Or is it that none of the JdbcTemplate objects start a transaction? Since JdbcTemplate is created within the method and not prior?

DB2 "With UR" Spring Data JPA

I am using a Spring Data Repository interface to retrieve data from DB2 (z/OS). I have a couple of methods that rely on the method signatues and one that relies on an explicit #Query.
How do I make the SQL generated have the "WITH UR" clause?
I added #Transactional(isolation=Isolation.READ_UNCOMMITTED) on the line above the #Query annotation and also above the two methods but it doesn't seem to append the clause to the generated SQL.
Thanks

Is a Spring Data JPA #Query dynamic or named?

JPA #NamedQuery is translated to SQL only once when application is deployed and generated SQL is cached.
EntityManager.createQuery translates query every time our method is called.
What Spring-data-jpa is doing with query defined in #Query annotation? Is it translated to SQL once during deployment (like NamedQuery) or translated every time (like dynamic query) ?
Spring Data JPA calls EntityManager.createQuery(…) for every invocation of a query method annotated with #Query. The reason for that is quite simple: the Query instances returned by the EntityManager are not thread-safe and actually stateful as they contain bound parameter information.
That said, most of the JPA persistence provider perform some kind of text-based query caching anyway so that they basically build the actual SQL query once for a certain JPQL query and reuse the former on subsequent invocations.
As an interesting side note, when we started building the support for #Query in 2008 we looked into possibilities to rather register the manually declared queries as named queries with JPA. Unfortunately there wasn't - and up until today - there's no way to manually register a named query programmatically via the JPA.

Resources