This example is using JPQL and executing select query to get multiple result from table. Here Employee is the name of entity class not a table name in database and emp is a alias for Employee entity. Query object is used to represent a query and it has getResultList method that returns list of multiple entities for each row fetched from database using JPA.
import java.util.*;
import javax.persistence.*;
public class JavaApp2
{
public static void main(String ar[]){
EntityManagerFactory emf=Persistence.createEntityManagerFactory("persistunit");
EntityManager em=emf.createEntityManager();
Query q=em.createQuery("SELECT OBJECT(emp) FROM Employee emp");
List<entities.Employee> rl=q.getResultList();
for(entities.Employee e:rl){
System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - ");
System.out.println("\t"+e.getId()+"\t"+e.getName()+"\t"+e.getSalary()+"\t"+e.getDesignation());
}
}
}
Generating Primary keys in JPA
import java.util.*;
import javax.persistence.*;
public class JavaApp2
{
public static void main(String ar[]){
EntityManagerFactory emf=Persistence.createEntityManagerFactory("persistunit");
EntityManager em=emf.createEntityManager();
Query q=em.createQuery("SELECT OBJECT(emp) FROM Employee emp");
List<entities.Employee> rl=q.getResultList();
for(entities.Employee e:rl){
System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - ");
System.out.println("\t"+e.getId()+"\t"+e.getName()+"\t"+e.getSalary()+"\t"+e.getDesignation());
}
}
}
Generating Primary keys in JPA
Comments