I'm wondering what is the purpose of native named queries in JPA. Let's have and example:
Utility class holding native queries:
class ExampleNativeQueries {
static final String NATIVE_QUERY = "select * from EXAMPLE_TABLE where EXAMPLE_VALUE = :value";
}
and Entity class + Repository:
@Entity
@Table(name = "EXAMPLE_TABLE")
public class Example {
@Id
private Long id;
@Column(name = "EXAMPLE_VALUE")
private String value;
}
interface ExampleRepository extends JpaRepository<Example, Long> {
@Query(value = ExampleNativeQueries.NATIVE_QUERY, nativeQuery = true)
List<Example> findByExampleValue(@Param("value") String value);
}
vs
@Entity
@Table(name = "EXAMPLE_TABLE")
@NamedNativeQuery(name = "Example.findByExampleValue", query = ExampleNativeQueries.NATIVE_QUERY, resultClass = Example.class)
public class Example {
@Id
private Long id;
@Column(name = "EXAMPLE_VALUE")
private String value;
}
interface ExampleRepository extends JpaRepository<Example, Long> {
@Query(nativeQuery = true)
List<Example> findByExampleValue(@Param("value") String value);
}
Is there any difference with that 2 approaches?