JPA - Scalar Function



Scalar functions return resultant values based on input values. For example, Upper() function returns the passed column value in upper case.

Following the same example employee management used in previous chapters, we will go through the service classes using scalar functions of JPQL.

Example - Usage of Upper() Scalar Function using JPA

Create a class named ScalarFunction.java under com.tutorialspoint.eclipselink.service package as follows−

ScalarFunction.java

package com.tutorialspoint.eclipselink.service;

import java.util.List;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import jakarta.persistence.Query;

public class ScalarFunction {
   public static void main( String[ ] args ) {
   
      EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "Eclipselink_JPA" );
      EntityManager entitymanager = emfactory.createEntityManager();

      //Scalar function
      Query query = entitymanager.createQuery("Select UPPER(e.ename) from Employee e");
      List<String> list = query.getResultList();

      for(String e:list) {
         System.out.println("Employee NAME :"+e);
      }
   }
}

Output

Run the code as Java Application in Eclipse IDE.

After compilation and execution of the above program you will get following result on the console panel of eclipse IDE.

Employee NAME :GOPAL
Employee NAME :MANISHA
Employee NAME :MASTHANVALI
Employee NAME :SATISH
Employee NAME :KRISHNA
Employee NAME :KIRAN
Advertisements