Java Tutorial/JPA/Named Query
Содержание
EJB QL GroupBy
File: Main.java
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Main {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
static EntityManager em = emf.createEntityManager();
public static void main(String[] a) throws Exception {
em.getTransaction().begin();
Query query = em.createQuery("SELECT d.name, COUNT(e) FROM Department d JOIN d.students e GROUP BY d.name");
System.out.println(query.getResultList());
em.getTransaction().rumit();
em.close();
emf.close();
Helper.checkData();
}
}
EJB QL Group By And Aggregate Function
File: Main.java
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FlushModeType;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Main {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
static EntityManager em = emf.createEntityManager();
public static void main(String[] a) throws Exception {
em.getTransaction().begin();
Query query = em.createQuery("SELECT d.name, AVG(e.salary) FROM Department d JOIN d.students e GROUP BY d.name");
System.out.println(query.getResultList());
em.getTransaction().rumit();
em.close();
emf.close();
Helper.checkData();
}
}
EJB QL Group Named Query With Named Queries
File: Main.java
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
public class Main {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
static EntityManager em = emf.createEntityManager();
public static void main(String[] a) throws Exception {
em.getTransaction().begin();
int id = 1;
try {
Student s = (Student) em.createNamedQuery("Student.findByPrimaryKey").setParameter("id", 1)
.getSingleResult();
} catch (NoResultException e) {
}
em.getTransaction().rumit();
em.close();
emf.close();
Helper.checkData();
}
}
EJB QL MemberOf function
File: Main.java
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FlushModeType;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Main {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
static EntityManager em = emf.createEntityManager();
public static void main(String[] a) throws Exception {
em.getTransaction().begin();
Query query = em.createQuery("SELECT e FROM Student e WHERE e MEMBER OF e.directs");
System.out.println(query.getResultList());
em.getTransaction().rumit();
em.close();
emf.close();
Helper.checkData();
}
}
Named Query With Int Parameter
File: Main.java
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
public class Main {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
static EntityManager em = emf.createEntityManager();
public static void main(String[] a) throws Exception {
em.getTransaction().begin();
int id = 1;
try {
Student s = (Student) em.createNamedQuery("Student.findByPrimaryKey").setParameter("id", 1)
.getSingleResult();
} catch (NoResultException e) {
}
em.getTransaction().rumit();
em.close();
emf.close();
Helper.checkData();
}
}
Named Query With String Parameter
File: Main.java
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
public class Main {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
static EntityManager em = emf.createEntityManager();
public static void main(String[] a) throws Exception {
em.getTransaction().begin();
String name = "Mark";
try {
Student s = (Student) em.createNamedQuery("Student.findByName").setParameter("name", name)
.getSingleResult();
} catch (NoResultException e) {
}
em.getTransaction().rumit();
em.close();
emf.close();
Helper.checkData();
}
}