Search code examples
spring-boothibernatehibernate-mapping

Named native query mapping in orm.xml


I am very new to Hibernate. I am trying to externalize basic SQL query to orm.xml

My query

SELECT CURRENT_TIMESTAMP  FROM DUAL

I have tried following :

<named-native-query 
    name="JobTrackerEntity.getCurrentTimestamp"
    result-class="java.sql.Timestamp">
    <query>
        SELECT CURRENT_TIMESTAMP  FROM DUAL
    </query>
</named-native-query>

But I get following error:

org.hibernate.MappingException: Unknown entity: java.sql.Timestamp

Please let me know how can I write the query in orm.xml. Also if I can refer any resources to get better understanding.


Update:

I have added a method in repository interface as follows:

@Repository
public interface TrackerRepository extends JpaRepository<TrackerEntity, TrackerId> {

    @Query(nativeQuery=true)
    Timestamp getCurrentTimestamp();
}

I'm calling getCurrentTimestamp() to execute query.


Solution

  • You must remove result-class="java.sql.Timestamp" because the result-class property must be an Entity.

    edit:

    CURRENT_TIMESTAMP results in a missing mapping in Hibernate dialect. You must provide your own dialect or if you don't need the timezone you better use:

    SELECT LOCALTIMESTAMP FROM DUAL