JPA教程 – JPA查询OrderBy两个属性示例 以下代码显示如何按两个属性排序,一个升序,另一个降序。 List l = em.createQuery("SELECT e FROM Professor e " + "JOIN e.department d ORDER BY d.name, e.name DESC") .getResultList(); 例子 以下代码来自Professor.java。 package cn.w3cschool.common; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity public class Professor { @Id private int id; private String name; private long salary; @Temporal(TemporalType.DATE) private Date startDate; @OneToOne private Address address; @OneToMany(mappedBy="employee") private Collection<Phone> phones = new ArrayList<Phone>(); @ManyToOne private Department department; @ManyToOne private Professor manager; @OneToMany(mappedBy="manager") private Collection<Professor> directs = new ArrayList<Professor>(); @ManyToMany(mappedBy="employees") private Collection<Project> projects = new ArrayList<Project>(); public int getId() { return id; } public void setId(int empNo) { this.id = empNo; } public String getName()...
JPA教程 – JPA查询命名参数示例 JPQL支持两种类型的参数绑定语法。 第一种是位置绑定,其中参数在查询字符串中通过问号后面跟随参数号来指示。 执行查询时,开发人员指定应替换的参数编号。 SELECT e FROM Employee e WHERE e.department = ?1 AND e.salary > ?2 命名参数在查询字符串中用冒号后跟参数名称指示。 当执行查询时,开发人员指定应该替换的参数名称。 这种类型的参数允许更具描述性的参数说明符。 SELECT e FROM Employee e WHERE e.department = :dept AND e.salary > :base 以下代码显示如何使用命名参数。 List l = em.createQuery( "SELECT e " + "FROM Professor e " + "WHERE e.department = :dept AND " + " e.salary = (SELECT MAX(e2.salary) " + " FROM Professor e2 " + " WHERE e2.department = :dept)") .setParameter("dept", d) .getResultList(); 例子 以下代码来自Address.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Address { @Id private int id; private String street; private String city; private String state; private String zip; public int getId() { return id; } public void setId(int id) { this.id =...
JPA教程 – JPA 查询Exists示例 如果子查询返回任何行,则EXISTS条件返回true。 以下代码显示如何在JPQL中使用带有子查询的EXISTS运算符。 List l = em.createQuery( "SELECT e FROM Professor e WHERE EXISTS " + "(SELECT p FROM Phone p WHERE p.employee = e)") .getResultList(); 以下代码显示如何使用NOT EXISTS。 List l = em.createQuery( "SELECT e FROM Professor e WHERE NOT EXISTS (SELECT p FROM e.phones p)") .getResultList(); 例子 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { prepareData(); List l = em.createQuery( "SELECT e FROM Professor e WHERE EXISTS " + "(SELECT p FROM Phone p WHERE p.employee = e)") .getResultList(); for (Object p : l) { printResult(p); } } private void prepareData() { Professor p = new Professor(); p.setId(0); p.setName("TOM"); p.setSalary(1111L); Department d = new Department();...