'Spring Data Jpa @query behaviours

Can anybody please tell me @Query annotation will support DB independence query mechanism

Example:

public interface UserRepository extends JpaRepository<User, Long> {

  @Query("select u from User u where u.firstname like %?1")
  List<User> findByFirstnameEndsWith(String firstname);
} 

if i write this query will it support all the DBs like Mysql,oracle, postgres.

i found something like this in spring data jpa reference document site that

@Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?1", nativeQuery = true)
  User findByEmailAddress(String emailAddress);

here is this means if i write nativeQuery=true it will treat as native query and if don't writer it will behave like a spring data jpa specific query or how it will behave please clarify.



Solution 1:[1]

If you set the nativeQuery flag to true, the query is treated as native sql whose behaviour will depend on DB host.If you dont set nativeQuery=true, then it is treated as JPQL and the real query for DB is generated by JPA according to the DB host hence it is DB independent.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rishabh