[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 !!

Leave a Reply

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial