[Workaround] NotYetImplementedException: Pure native scalar queries are not yet supported

You may get NotYetImplementedException exception when you are trying to include some native SQL queries in your project as named native queries, and you do not expect any result after execution of those sql queries. e.g. SQL UPDATE queries which does not return any result.

Problem

Let’s say you want to write an update query to update some employee details like this:

@Entity(name="EmployeeEntity")
@Table (name="employee")

@NamedNativeQueries({
		@NamedNativeQuery(
				name	=	"updateEmployeeName",
				query	=	"UPDATE employee SET firstName = ?, lastName = ? WHERE id = ?"
		)
})

public class EmployeeEntity implements Serializable
{
	//entity code
}

When you will try to update EmployeeEntity using above named native query, it will throw the exception as below:

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: demoJPAUnit] Unable to build EntityManagerFactory
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)
	at org.springframework.orm.jpa.vendor.SpringHibernateEjbPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateEjbPersistenceProvider.java:51)
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
	... 40 more
Caused by: org.hibernate.cfg.NotYetImplementedException: Pure native scalar queries are not yet supported
	at org.hibernate.cfg.annotations.QueryBinder.bindNativeQuery(QueryBinder.java:145)
	at org.hibernate.cfg.annotations.QueryBinder.bindNativeQueries(QueryBinder.java:226)
	at org.hibernate.cfg.AnnotationBinder.bindQueries(AnnotationBinder.java:345)
	at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:545)
	at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3406)
	at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3360)
	at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1334)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1724)
	at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84)
	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
	... 46 more

Workaround

exceptions-notes

To fix this annoying problem (rather should be limitation), add one dummy result set mapping to satisfy hibernate configuration validators.

@Entity(name="EmployeeEntity")
@Table (name="employee")

@SqlResultSetMapping(name="updateResult", columns = { @ColumnResult(name = "count")})

@NamedNativeQueries({
		@NamedNativeQuery(
				name	=	"updateEmployeeName",
				query	=	"UPDATE employee SET firstName = ?, lastName = ? WHERE id = ?"
				,resultSetMapping = "updateResult"
		)
})

public class EmployeeEntity implements Serializable
{
	//entity code
}	

Now the program will execute as you are making it to run.

Drop me your questions and comments.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

2 thoughts on “[Workaround] NotYetImplementedException: Pure native scalar queries are not yet supported”

  1. Hi,

    Just wanted to say thanks. This solved my Problem (years later haha).

    Just wanted to add that if the query returns 0.xx entity columns you could also specify a result class and the query would return a list of Entities with only the specified column filled.

    Regards,

    Kim

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.