Hibernate Error - org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported



You can see "pure native scalar queries are not yet supported" this error message in Hibernate very often.
Exception in thread "main" org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported
 at org.hibernate.cfg.annotations.QueryBinder.bindNativeQuery(QueryBinder.java:118)
 at org.hibernate.cfg.annotations.QueryBinder.bindNativeQueries(QueryBinder.java:197)
 at org.hibernate.cfg.AnnotationBinder.bindQueries(AnnotationBinder.java:287)
 at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:417)
 at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:498)
 at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:277)
 at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
 at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
 at com.sample.hibernate.driver.HibernateRunner.main(HibernateRunner.java:52)

This is a common error when you use "Native SQL Query" in Hibernate. If you miss out resultClass in @NamedNativeQuery, Hibernate will unaware of the return type of this query. So it will throw org.hibernate.cfg.NotYetImplementedException.

Correct way of creating NamedNativeQuery is;
@NamedNativeQueries({
@NamedNativeQuery(
 name = "getPersons",
 query = "select * from persons",
        resultClass = Persons.class
 )
})
Incorrect way of creating NamedNativeQuery;

@NamedNativeQueries({
@NamedNativeQuery(
name = "getPersons",
 query = "select * from persons"
 )
})
Sample Entity class is given below;
package com.sample.hibernate.dto;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.Table;

@NamedNativeQueries({
 @NamedNativeQuery(
 name = "getPersons",
 query = "select * from persons",
    resultClass = Persons.class
 )
})
@Entity
@Table(name = "PERSONS", schema = "PUBLIC")
public class Persons {
 private int pid;
 private String firstName;
 private String lastName;
 private String address;
 private String city;

 /**
  * @return the pid
  */
 @Id
 @Column(name = "P_ID", unique = true, nullable = false)
 public int getPid() {
  return pid;
 }

 /**
  * @param pid
  *            the pid to set
  */
 public void setPid(int pid) {
  this.pid = pid;
 }

 /**
  * @return the firstName
  */
 @Column(name = "FIRSTNAME")
 public String getFirstName() {
  return firstName;
 }

 /**
  * @param firstName
  *            the firstName to set
  */
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 /**
  * @return the lastName
  */
 @Column(name = "LASTNAME")
 public String getLastName() {
  return lastName;
 }

 /**
  * @param lastName
  *            the lastName to set
  */
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 /**
  * @return the address
  */
 @Column(name = "ADDRESS")
 public String getAddress() {
  return address;
 }

 /**
  * @param address
  *            the address to set
  */
 public void setAddress(String address) {
  this.address = address;
 }

 /**
  * @return the city
  */
 @Column(name = "CITY")
 public String getCity() {
  return city;
 }

 /**
  * @param city
  *            the city to set
  */
 public void setCity(String city) {
  this.city = city;
 }

 @Override
 public String toString() {
  return String.format(
    "id=%d\nFirst name=%s\nLast name=%s\nAddress=%s\nCity=%s",
    this.pid, this.firstName, this.lastName, this.address,
    this.city);
 }

}





Reactions:

0 comments :

Post a Comment