JPA 基本查询(五)

ops/2024/12/29 9:05:51/

JPA 查询Exists示例

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();d.setId(1);d.setName("Design");p.setDepartment(d);d.getProfessors().add(p);Phone phone = new Phone();phone.setId(1);phone.setNumber("111-111-1111");phone.setProfessor(p);em.persist(p);em.persist(phone);em.persist(d);}private static void printResult(Object result) {if (result == null) {System.out.print("NULL");} else if (result instanceof Object[]) {Object[] row = (Object[]) result;System.out.print("[");for (int i = 0; i < row.length; i++) {printResult(row[i]);}System.out.print("]");} else if (result instanceof Long || result instanceof Double|| result instanceof String) {System.out.print(result.getClass().getName() + ": " + result);} else {System.out.print(result);}System.out.println();}@PersistenceContextprivate EntityManager em;
}

下面的代码来自Department.java

package cn.w3cschool.common;import java.util.HashSet;
import java.util.Set;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;@Entity
public class Department {@Idprivate int id;private String name;@OneToMany(mappedBy="department")private Set<Professor> employees = new HashSet<Professor>();public int getId() {return id;}public void setId(int deptNo) {this.id = deptNo;}public String getName() {return name;}public void setName(String deptName) {this.name = deptName;}public Set<Professor> getProfessors() {return employees;}public String toString() {return "Department no: " + getId() + ", name: " + getName();}
}

以下代码来自Address.java

package cn.w3cschool.common;import javax.persistence.Entity;
import javax.persistence.Id;@Entity
public class Address {@Idprivate 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 = id;}public String getStreet() {return street;}public void setStreet(String address) {this.street = address;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getState() {return state;}public void setState(String state) {this.state = state;}public String getZip() {return zip;}public void setZip(String zip) {this.zip = zip;}public String toString() {return "Address id: " + getId() + ", street: " + getStreet() +", city: " + getCity() +", state: " + getState() +", zip: " + getZip();}}

以下代码来自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 {@Idprivate int id;private String name;private long salary;@Temporal(TemporalType.DATE)private Date startDate;@OneToOneprivate Address address;@OneToMany(mappedBy="employee")private Collection<Phone> phones = new ArrayList<Phone>();@ManyToOneprivate Department department;@ManyToOneprivate 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() {return name;}public void setName(String name) {this.name = name;}public long getSalary() {return salary;}public void setSalary(long salary) {this.salary = salary;}public Date getStartDate() {return startDate;}public void setStartDate(Date startDate) {this.startDate = startDate;}public Collection<Phone> getPhones() {return phones;}public void addPhone(Phone phone) {if (!getPhones().contains(phone)) {getPhones().add(phone);if (phone.getProfessor() != null) {phone.getProfessor().getPhones().remove(phone);}phone.setProfessor(this);}}public Department getDepartment() {return department;}public void setDepartment(Department department) {if (this.department != null) {this.department.getProfessors().remove(this);}this.department = department;this.department.getProfessors().add(this);}public Collection<Professor> getDirects() {return directs;}public void addDirect(Professor employee) {if (!getDirects().contains(employee)) {getDirects().add(employee);if (employee.getManager() != null) {employee.getManager().getDirects().remove(employee);}employee.setManager(this);}}public Professor getManager() {return manager;}public void setManager(Professor manager) {this.manager = manager;}public Collection<Project> getProjects() {return projects;}public void addProject(Project project) {if (!getProjects().contains(project)) {getProjects().add(project);}if (!project.getProfessors().contains(this)) {project.getProfessors().add(this);}}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address; }public String toString() {return "Professor " + getId() + ": name: " + getName() +", salary: " + getSalary() +", phones: " + getPhones() +", managerNo: " + ((getManager() == null) ? null : getManager().getId()) +", deptNo: " + ((getDepartment() == null) ? null : getDepartment().getId());}}

以下代码来自Project.java

package cn.w3cschool.common;import java.util.ArrayList;
import java.util.Collection;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.ManyToMany;@Entity
@Inheritance
public class Project {@Idprotected int id;protected String name;@ManyToManyprotected Collection<Professor> employees = new ArrayList<Professor>();public int getId() {return id;}public void setId(int projectNo) {this.id = projectNo;}public String getName() {return name;}public void setName(String projectName) {this.name = projectName;}public Collection<Professor> getProfessors() {return employees;}public void addProfessor(Professor employee) {if (!getProfessors().contains(employee)) {getProfessors().add(employee);}if (!employee.getProjects().contains(this)) {employee.getProjects().add(this);}}public String toString() {return getClass().getName().substring(getClass().getName().lastIndexOf(".")+1) + " no: " + getId() + ", name: " + getName();}
}

以下代码来自Phone.java

package cn.w3cschool.common;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;@Entity
public class Phone {@Idprivate long id;private String number;private String type;@ManyToOneProfessor employee;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getNumber() {return number;}public void setNumber(String phoneNo) {this.number = phoneNo;}public String getType() {return type;}public void setType(String phoneType) {this.type = phoneType;}public Professor getProfessor() {return employee;}public void setProfessor(Professor employee) {this.employee = employee;}public String toString() {return "Phone id: " + getId() + ", no: " + getNumber() +", type: " + getType();}
}

下载 Query_Exists.zip
下载 Query_Not_Exists.zip

以下是数据库转储。

Table Name: ADDRESSTable Name: DEPARTMENTRow:Column Name: ID,Column Type: INTEGER:Column Value: 1Column Name: NAME,Column Type: VARCHAR:Column Value: DesignTable Name: PHONERow:Column Name: ID,Column Type: BIGINT:Column Value: 1Column Name: NUMBER,Column Type: VARCHAR:Column Value: 111-111-1111Column Name: TYPE,Column Type: VARCHAR:Column Value: nullColumn Name: EMPLOYEE_ID,Column Type: INTEGER:Column Value: 0Table Name: PROFESSORRow:Column Name: ID,Column Type: INTEGER:Column Value: 0Column Name: NAME,Column Type: VARCHAR:Column Value: TOMColumn Name: SALARY,Column Type: BIGINT:Column Value: 1111Column Name: STARTDATE,Column Type: DATE:Column Value: nullColumn Name: ADDRESS_ID,Column Type: INTEGER:Column Value: nullColumn Name: DEPARTMENT_ID,Column Type: INTEGER:Column Value: 1Column Name: MANAGER_ID,Column Type: INTEGER:Column Value: nullTable Name: PROJECTTable Name: PROJECT_PROFESSOR

JPA 查询AVG简单示例

JPA教程 - JPA查询AVG简单示例

JPQL中的聚合查询的语法与SQL的语法非常相似。

有五个支持的聚合函数

  • AVG
  • COUNT
  • MIN
  • MAX
  • SUM

结果可以分组在GROUP BY子句中,并使用HAVING子句进行过滤。

聚合JPQL查询可以使用同一查询中的许多聚合函数:

SELECT d, COUNT(e), MAX(e.salary), AVG(e.salary)
FROM Department d JOIN d.employees e
GROUP BY d
HAVING COUNT(e) >= 5

JPQL中的AVG函数计算属性的平均值。

以下行计算平均工资。

"SELECT AVG(e.salary) FROM Professor e")

聚合查询

聚合查询对结果进行分组,并应用聚合函数以获取有关查询结果的摘要信息。

聚合查询的语法如下:

 
SELECT <select_expression>
FROM <from_clause>
[WHERE <conditional_expression>]
[GROUP BY <group_by_clause>]
[HAVING <conditional_expression>]
[ORDER BY <order_by_clause>]

例子

下面的代码来自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 AVG(e.salary) FROM Professor 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();d.setId(1);d.setName("Design");p.setDepartment(d);d.getProfessors().add(p);Phone phone = new Phone();phone.setId(1);phone.setNumber("111-111-1111");phone.setProfessor(p);em.persist(p);em.persist(phone);em.persist(d);}private static void printResult(Object result) {if (result == null) {System.out.print("NULL");} else if (result instanceof Object[]) {Object[] row = (Object[]) result;System.out.print("[");for (int i = 0; i < row.length; i++) {printResult(row[i]);}System.out.print("]");} else if (result instanceof Long || result instanceof Double|| result instanceof String) {System.out.print(result.getClass().getName() + ": " + result);} else {System.out.print(result);}System.out.println();}@PersistenceContextprivate EntityManager em;
}

以下代码来自Project.java

package cn.w3cschool.common;import java.util.ArrayList;
import java.util.Collection;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.ManyToMany;@Entity
@Inheritance
public class Project {@Idprotected int id;protected String name;@ManyToManyprotected Collection<Professor> employees = new ArrayList<Professor>();public int getId() {return id;}public void setId(int projectNo) {this.id = projectNo;}public String getName() {return name;}public void setName(String projectName) {this.name = projectName;}public Collection<Professor> getProfessors() {return employees;}public void addProfessor(Professor employee) {if (!getProfessors().contains(employee)) {getProfessors().add(employee);}if (!employee.getProjects().contains(this)) {employee.getProjects().add(this);}}public String toString() {return getClass().getName().substring(getClass().getName().lastIndexOf(".")+1) + " no: " + getId() + ", name: " + getName();}
}

以下代码来自Phone.java

package cn.w3cschool.common;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;@Entity
public class Phone {@Idprivate long id;private String number;private String type;@ManyToOneProfessor employee;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getNumber() {return number;}public void setNumber(String phoneNo) {this.number = phoneNo;}public String getType() {return type;}public void setType(String phoneType) {this.type = phoneType;}public Professor getProfessor() {return employee;}public void setProfessor(Professor employee) {this.employee = employee;}public String toString() {return "Phone id: " + getId() + ", no: " + getNumber() +", type: " + getType();}
}

以下代码来自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 {@Idprivate int id;private String name;private long salary;@Temporal(TemporalType.DATE)private Date startDate;@OneToOneprivate Address address;@OneToMany(mappedBy="employee")private Collection<Phone> phones = new ArrayList<Phone>();@ManyToOneprivate Department department;@ManyToOneprivate 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() {return name;}public void setName(String name) {this.name = name;}public long getSalary() {return salary;}public void setSalary(long salary) {this.salary = salary;}public Date getStartDate() {return startDate;}public void setStartDate(Date startDate) {this.startDate = startDate;}public Collection<Phone> getPhones() {return phones;}public void addPhone(Phone phone) {if (!getPhones().contains(phone)) {getPhones().add(phone);if (phone.getProfessor() != null) {phone.getProfessor().getPhones().remove(phone);}phone.setProfessor(this);}}public Department getDepartment() {return department;}public void setDepartment(Department department) {if (this.department != null) {this.department.getProfessors().remove(this);}this.department = department;this.department.getProfessors().add(this);}public Collection<Professor> getDirects() {return directs;}public void addDirect(Professor employee) {if (!getDirects().contains(employee)) {getDirects().add(employee);if (employee.getManager() != null) {employee.getManager().getDirects().remove(employee);}employee.setManager(this);}}public Professor getManager() {return manager;}public void setManager(Professor manager) {this.manager = manager;}public Collection<Project> getProjects() {return projects;}public void addProject(Project project) {if (!getProjects().contains(project)) {getProjects().add(project);}if (!project.getProfessors().contains(this)) {project.getProfessors().add(this);}}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address; }public String toString() {return "Professor " + getId() + ": name: " + getName() +", salary: " + getSalary() +", phones: " + getPhones() +", managerNo: " + ((getManager() == null) ? null : getManager().getId()) +", deptNo: " + ((getDepartment() == null) ? null : getDepartment().getId());}}

以下代码来自Address.java

package cn.w3cschool.common;import javax.persistence.Entity;
import javax.persistence.Id;@Entity
public class Address {@Idprivate 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 = id;}public String getStreet() {return street;}public void setStreet(String address) {this.street = address;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getState() {return state;}public void setState(String state) {this.state = state;}public String getZip() {return zip;}public void setZip(String zip) {this.zip = zip;}public String toString() {return "Address id: " + getId() + ", street: " + getStreet() +", city: " + getCity() +", state: " + getState() +", zip: " + getZip();}}

下面的代码来自Department.java

package cn.w3cschool.common;import java.util.HashSet;
import java.util.Set;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;@Entity
public class Department {@Idprivate int id;private String name;@OneToMany(mappedBy="department")private Set<Professor> employees = new HashSet<Professor>();public int getId() {return id;}public void setId(int deptNo) {this.id = deptNo;}public String getName() {return name;}public void setName(String deptName) {this.name = deptName;}public Set<Professor> getProfessors() {return employees;}public String toString() {return "Department no: " + getId() + ", name: " + getName();}
}

下载 Query_AVG_Simple.zip

上面的代码生成以下结果。

以下是数据库转储。

Table Name: ADDRESSTable Name: DEPARTMENTRow:Column Name: ID,Column Type: INTEGER:Column Value: 1Column Name: NAME,Column Type: VARCHAR:Column Value: DesignTable Name: PHONERow:Column Name: ID,Column Type: BIGINT:Column Value: 1Column Name: NUMBER,Column Type: VARCHAR:Column Value: 111-111-1111Column Name: TYPE,Column Type: VARCHAR:Column Value: nullColumn Name: EMPLOYEE_ID,Column Type: INTEGER:Column Value: 0Table Name: PROFESSORRow:Column Name: ID,Column Type: INTEGER:Column Value: 0Column Name: NAME,Column Type: VARCHAR:Column Value: TOMColumn Name: SALARY,Column Type: BIGINT:Column Value: 1111Column Name: STARTDATE,Column Type: DATE:Column Value: nullColumn Name: ADDRESS_ID,Column Type: INTEGER:Column Value: nullColumn Name: DEPARTMENT_ID,Column Type: INTEGER:Column Value: 1Column Name: MANAGER_ID,Column Type: INTEGER:Column Value: nullTable Name: PROJECTTable Name: PROJECT_PROFESSOR

JPA 查询AVG连接GroupBy示例

JPA教程 - JPA查询AVG连接GroupBy示例

以下代码显示如何在JPQL中使用连接子句使用AVG。

List l = em.createQuery("SELECT d.name, AVG(e.salary) FROM Department d JOIN d.employees e GROUP BY d.name").getResultList();

例子

以下代码来自Address.java

package cn.w3cschool.common;import javax.persistence.Entity;
import javax.persistence.Id;@Entity
public class Address {@Idprivate 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 = id;}public String getStreet() {return street;}public void setStreet(String address) {this.street = address;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getState() {return state;}public void setState(String state) {this.state = state;}public String getZip() {return zip;}public void setZip(String zip) {this.zip = zip;}public String toString() {return "Address id: " + getId() + ", street: " + getStreet() +", city: " + getCity() +", state: " + getState() +", zip: " + getZip();}}

下面的代码来自Department.java

package cn.w3cschool.common;import java.util.HashSet;
import java.util.Set;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;@Entity
public class Department {@Idprivate int id;private String name;@OneToMany(mappedBy="department")private Set<Professor> employees = new HashSet<Professor>();public int getId() {return id;}public void setId(int deptNo) {this.id = deptNo;}public String getName() {return name;}public void setName(String deptName) {this.name = deptName;}public Set<Professor> getProfessors() {return employees;}public String toString() {return "Department no: " + getId() + ", name: " + getName();}
}

以下代码来自Phone.java

package cn.w3cschool.common;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;@Entity
public class Phone {@Idprivate long id;private String number;private String type;@ManyToOneProfessor employee;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getNumber() {return number;}public void setNumber(String phoneNo) {this.number = phoneNo;}public String getType() {return type;}public void setType(String phoneType) {this.type = phoneType;}public Professor getProfessor() {return employee;}public void setProfessor(Professor employee) {this.employee = employee;}public String toString() {return "Phone id: " + getId() + ", no: " + getNumber() +", type: " + getType();}
}

以下代码来自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 {@Idprivate int id;private String name;private long salary;@Temporal(TemporalType.DATE)private Date startDate;@OneToOneprivate Address address;@OneToMany(mappedBy="employee")private Collection<Phone> phones = new ArrayList<Phone>();@ManyToOneprivate Department department;@ManyToOneprivate 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() {return name;}public void setName(String name) {this.name = name;}public long getSalary() {return salary;}public void setSalary(long salary) {this.salary = salary;}public Date getStartDate() {return startDate;}public void setStartDate(Date startDate) {this.startDate = startDate;}public Collection<Phone> getPhones() {return phones;}public void addPhone(Phone phone) {if (!getPhones().contains(phone)) {getPhones().add(phone);if (phone.getProfessor() != null) {phone.getProfessor().getPhones().remove(phone);}phone.setProfessor(this);}}public Department getDepartment() {return department;}public void setDepartment(Department department) {if (this.department != null) {this.department.getProfessors().remove(this);}this.department = department;this.department.getProfessors().add(this);}public Collection<Professor> getDirects() {return directs;}public void addDirect(Professor employee) {if (!getDirects().contains(employee)) {getDirects().add(employee);if (employee.getManager() != null) {employee.getManager().getDirects().remove(employee);}employee.setManager(this);}}public Professor getManager() {return manager;}public void setManager(Professor manager) {this.manager = manager;}public Collection<Project> getProjects() {return projects;}public void addProject(Project project) {if (!getProjects().contains(project)) {getProjects().add(project);}if (!project.getProfessors().contains(this)) {project.getProfessors().add(this);}}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address; }public String toString() {return "Professor " + getId() + ": name: " + getName() +", salary: " + getSalary() +", phones: " + getPhones() +", managerNo: " + ((getManager() == null) ? null : getManager().getId()) +", deptNo: " + ((getDepartment() == null) ? null : getDepartment().getId());}}

以下代码来自Project.java

package cn.w3cschool.common;import java.util.ArrayList;
import java.util.Collection;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.ManyToMany;@Entity
@Inheritance
public class Project {@Idprotected int id;protected String name;@ManyToManyprotected Collection<Professor> employees = new ArrayList<Professor>();public int getId() {return id;}public void setId(int projectNo) {this.id = projectNo;}public String getName() {return name;}public void setName(String projectName) {this.name = projectName;}public Collection<Professor> getProfessors() {return employees;}public void addProfessor(Professor employee) {if (!getProfessors().contains(employee)) {getProfessors().add(employee);}if (!employee.getProjects().contains(this)) {employee.getProjects().add(this);}}public String toString() {return getClass().getName().substring(getClass().getName().lastIndexOf(".")+1) + " no: " + getId() + ", name: " + getName();}
}

下面的代码来自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 d.name, AVG(e.salary) FROM Department d JOIN d.employees e GROUP BY d.name").getResultList();for (Object p : l) {printResult(p);}}private void prepareData() {Address address = new Address();address.setState("BC");Professor p = new Professor();p.setId(0);p.setName("TOM");p.setSalary(1111L);p.setAddress(address);Department d = new Department();d.setId(1);d.setName("Design");p.setDepartment(d);d.getProfessors().add(p);Phone phone = new Phone();phone.setId(1);phone.setNumber("111-111-1111");phone.setProfessor(p);em.persist(p);em.persist(phone);em.persist(address);em.persist(d);}private static void printResult(Object result) {if (result == null) {System.out.print("NULL");} else if (result instanceof Object[]) {Object[] row = (Object[]) result;System.out.print("[");for (int i = 0; i < row.length; i++) {printResult(row[i]);}System.out.print("]");} else if (result instanceof Long || result instanceof Double|| result instanceof String) {System.out.print(result.getClass().getName() + ": " + result);} else {System.out.print(result);}System.out.println();}@PersistenceContextprivate EntityManager em;
}

下载 Query_AVG_Join_GroupBy.zip

上面的代码生成以下结果。

以下是数据库转储。

Table Name: ADDRESSRow:Column Name: ID,Column Type: INTEGER:Column Value: 0Column Name: CITY,Column Type: VARCHAR:Column Value: nullColumn Name: STATE,Column Type: VARCHAR:Column Value: BCColumn Name: STREET,Column Type: VARCHAR:Column Value: nullColumn Name: ZIP,Column Type: VARCHAR:Column Value: nullTable Name: DEPARTMENTRow:Column Name: ID,Column Type: INTEGER:Column Value: 1Column Name: NAME,Column Type: VARCHAR:Column Value: DesignTable Name: PHONERow:Column Name: ID,Column Type: BIGINT:Column Value: 1Column Name: NUMBER,Column Type: VARCHAR:Column Value: 111-111-1111Column Name: TYPE,Column Type: VARCHAR:Column Value: nullColumn Name: EMPLOYEE_ID,Column Type: INTEGER:Column Value: 0Table Name: PROFESSORRow:Column Name: ID,Column Type: INTEGER:Column Value: 0Column Name: NAME,Column Type: VARCHAR:Column Value: TOMColumn Name: SALARY,Column Type: BIGINT:Column Value: 1111Column Name: STARTDATE,Column Type: DATE:Column Value: nullColumn Name: ADDRESS_ID,Column Type: INTEGER:Column Value: 0Column Name: DEPARTMENT_ID,Column Type: INTEGER:Column Value: 1Column Name: MANAGER_ID,Column Type: INTEGER:Column Value: nullTable Name: PROJECTTable Name: PROJECT_PROFESSOR

JPA 查询COUNT示例

JPA教程 - JPA查询COUNT示例

COUNT函数用于计算表中的元素。

 "SELECT d.name, COUNT(e), AVG(e.salary) "+ "FROM Department d JOIN d.employees e GROUP BY d.name")

例子

下面的代码来自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 d.name, COUNT(e), AVG(e.salary) "+ "FROM Department d JOIN d.employees e GROUP BY d.name").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();d.setId(1);d.setName("Design");p.setDepartment(d);d.getProfessors().add(p);Phone phone = new Phone();phone.setId(1);phone.setNumber("111-111-1111");phone.setProfessor(p);em.persist(p);em.persist(phone);em.persist(d);}private static void printResult(Object result) {if (result == null) {System.out.print("NULL");} else if (result instanceof Object[]) {Object[] row = (Object[]) result;System.out.print("[");for (int i = 0; i < row.length; i++) {printResult(row[i]);}System.out.print("]");} else if (result instanceof Long || result instanceof Double|| result instanceof String) {System.out.print(result.getClass().getName() + ": " + result);} else {System.out.print(result);}System.out.println();}@PersistenceContextprivate EntityManager em;
}

以下代码来自Project.java

package cn.w3cschool.common;import java.util.ArrayList;
import java.util.Collection;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.ManyToMany;@Entity
@Inheritance
public class Project {@Idprotected int id;protected String name;@ManyToManyprotected Collection<Professor> employees = new ArrayList<Professor>();public int getId() {return id;}public void setId(int projectNo) {this.id = projectNo;}public String getName() {return name;}public void setName(String projectName) {this.name = projectName;}public Collection<Professor> getProfessors() {return employees;}public void addProfessor(Professor employee) {if (!getProfessors().contains(employee)) {getProfessors().add(employee);}if (!employee.getProjects().contains(this)) {employee.getProjects().add(this);}}public String toString() {return getClass().getName().substring(getClass().getName().lastIndexOf(".")+1) + " no: " + getId() + ", name: " + getName();}
}

以下代码来自Address.java

package cn.w3cschool.common;import javax.persistence.Entity;
import javax.persistence.Id;@Entity
public class Address {@Idprivate 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 = id;}public String getStreet() {return street;}public void setStreet(String address) {this.street = address;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getState() {return state;}public void setState(String state) {this.state = state;}public String getZip() {return zip;}public void setZip(String zip) {this.zip = zip;}public String toString() {return "Address id: " + getId() + ", street: " + getStreet() +", city: " + getCity() +", state: " + getState() +", zip: " + getZip();}}

以下代码来自Phone.java

package cn.w3cschool.common;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;@Entity
public class Phone {@Idprivate long id;private String number;private String type;@ManyToOneProfessor employee;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getNumber() {return number;}public void setNumber(String phoneNo) {this.number = phoneNo;}public String getType() {return type;}public void setType(String phoneType) {this.type = phoneType;}public Professor getProfessor() {return employee;}public void setProfessor(Professor employee) {this.employee = employee;}public String toString() {return "Phone id: " + getId() + ", no: " + getNumber() +", type: " + getType();}
}

以下代码来自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 {@Idprivate int id;private String name;private long salary;@Temporal(TemporalType.DATE)private Date startDate;@OneToOneprivate Address address;@OneToMany(mappedBy="employee")private Collection<Phone> phones = new ArrayList<Phone>();@ManyToOneprivate Department department;@ManyToOneprivate 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() {return name;}public void setName(String name) {this.name = name;}public long getSalary() {return salary;}public void setSalary(long salary) {this.salary = salary;}public Date getStartDate() {return startDate;}public void setStartDate(Date startDate) {this.startDate = startDate;}public Collection<Phone> getPhones() {return phones;}public void addPhone(Phone phone) {if (!getPhones().contains(phone)) {getPhones().add(phone);if (phone.getProfessor() != null) {phone.getProfessor().getPhones().remove(phone);}phone.setProfessor(this);}}public Department getDepartment() {return department;}public void setDepartment(Department department) {if (this.department != null) {this.department.getProfessors().remove(this);}this.department = department;this.department.getProfessors().add(this);}public Collection<Professor> getDirects() {return directs;}public void addDirect(Professor employee) {if (!getDirects().contains(employee)) {getDirects().add(employee);if (employee.getManager() != null) {employee.getManager().getDirects().remove(employee);}employee.setManager(this);}}public Professor getManager() {return manager;}public void setManager(Professor manager) {this.manager = manager;}public Collection<Project> getProjects() {return projects;}public void addProject(Project project) {if (!getProjects().contains(project)) {getProjects().add(project);}if (!project.getProfessors().contains(this)) {project.getProfessors().add(this);}}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address; }public String toString() {return "Professor " + getId() + ": name: " + getName() +", salary: " + getSalary() +", phones: " + getPhones() +", managerNo: " + ((getManager() == null) ? null : getManager().getId()) +", deptNo: " + ((getDepartment() == null) ? null : getDepartment().getId());}}

下面的代码来自Department.java

package cn.w3cschool.common;import java.util.HashSet;
import java.util.Set;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;@Entity
public class Department {@Idprivate int id;private String name;@OneToMany(mappedBy="department")private Set<Professor> employees = new HashSet<Professor>();public int getId() {return id;}public void setId(int deptNo) {this.id = deptNo;}public String getName() {return name;}public void setName(String deptName) {this.name = deptName;}public Set<Professor> getProfessors() {return employees;}public String toString() {return "Department no: " + getId() + ", name: " + getName();}
}

下载 Query_AVG.zip

上面的代码生成以下结果。

以下是数据库转储。

Table Name: ADDRESSTable Name: DEPARTMENTRow:Column Name: ID,Column Type: INTEGER:Column Value: 1Column Name: NAME,Column Type: VARCHAR:Column Value: DesignTable Name: PHONERow:Column Name: ID,Column Type: BIGINT:Column Value: 1Column Name: NUMBER,Column Type: VARCHAR:Column Value: 111-111-1111Column Name: TYPE,Column Type: VARCHAR:Column Value: nullColumn Name: EMPLOYEE_ID,Column Type: INTEGER:Column Value: 0Table Name: PROFESSORRow:Column Name: ID,Column Type: INTEGER:Column Value: 0Column Name: NAME,Column Type: VARCHAR:Column Value: TOMColumn Name: SALARY,Column Type: BIGINT:Column Value: 1111Column Name: STARTDATE,Column Type: DATE:Column Value: nullColumn Name: ADDRESS_ID,Column Type: INTEGER:Column Value: nullColumn Name: DEPARTMENT_ID,Column Type: INTEGER:Column Value: 1Column Name: MANAGER_ID,Column Type: INTEGER:Column Value: nullTable Name: PROJECTTable Name: PROJECT_PROFESSOR

JPA 查询计数其中子查询示例

JPA教程 - JPA 查询计数其中子查询示例

以下代码显示如何在子查询中使用COUNT函数。

List l = em.createQuery("SELECT m FROM Professor m WHERE (SELECT COUNT(e) "+ "FROM Professor e WHERE e.manager = m) > 0").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 {@Idprivate int id;private String name;private long salary;@Temporal(TemporalType.DATE)private Date startDate;@OneToOneprivate Address address;@OneToMany(mappedBy="employee")private Collection<Phone> phones = new ArrayList<Phone>();@ManyToOneprivate Department department;@ManyToOneprivate 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() {return name;}public void setName(String name) {this.name = name;}public long getSalary() {return salary;}public void setSalary(long salary) {this.salary = salary;}public Date getStartDate() {return startDate;}public void setStartDate(Date startDate) {this.startDate = startDate;}public Collection<Phone> getPhones() {return phones;}public void addPhone(Phone phone) {if (!getPhones().contains(phone)) {getPhones().add(phone);if (phone.getProfessor() != null) {phone.getProfessor().getPhones().remove(phone);}phone.setProfessor(this);}}public Department getDepartment() {return department;}public void setDepartment(Department department) {if (this.department != null) {this.department.getProfessors().remove(this);}this.department = department;this.department.getProfessors().add(this);}public Collection<Professor> getDirects() {return directs;}public void addDirect(Professor employee) {if (!getDirects().contains(employee)) {getDirects().add(employee);if (employee.getManager() != null) {employee.getManager().getDirects().remove(employee);}employee.setManager(this);}}public Professor getManager() {return manager;}public void setManager(Professor manager) {this.manager = manager;}public Collection<Project> getProjects() {return projects;}public void addProject(Project project) {if (!getProjects().contains(project)) {getProjects().add(project);}if (!project.getProfessors().contains(this)) {project.getProfessors().add(this);}}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address; }public String toString() {return "Professor " + getId() + ": name: " + getName() +", salary: " + getSalary() +", phones: " + getPhones() +", managerNo: " + ((getManager() == null) ? null : getManager().getId()) +", deptNo: " + ((getDepartment() == null) ? null : getDepartment().getId());}}

下面的代码来自Department.java

package cn.w3cschool.common;import java.util.HashSet;
import java.util.Set;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;@Entity
public class Department {@Idprivate int id;private String name;@OneToMany(mappedBy="department")private Set<Professor> employees = new HashSet<Professor>();public int getId() {return id;}public void setId(int deptNo) {this.id = deptNo;}public String getName() {return name;}public void setName(String deptName) {this.name = deptName;}public Set<Professor> getProfessors() {return employees;}public String toString() {return "Department no: " + getId() + ", name: " + getName();}
}

下面的代码来自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 m FROM Professor m WHERE (SELECT COUNT(e) "+ "FROM Professor e WHERE e.manager = m) > 0").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();d.setId(1);d.setName("Design");p.setDepartment(d);d.getProfessors().add(p);em.persist(p);em.persist(d);}private static void printResult(Object result) {if (result == null) {System.out.print("NULL");} else if (result instanceof Object[]) {Object[] row = (Object[]) result;System.out.print("[");for (int i = 0; i < row.length; i++) {printResult(row[i]);}System.out.print("]");} else if (result instanceof Long || result instanceof Double|| result instanceof String) {System.out.print(result.getClass().getName() + ": " + result);} else {System.out.print(result);}System.out.println();}@PersistenceContextprivate EntityManager em;
}

以下代码来自Project.java

package cn.w3cschool.common;import java.util.ArrayList;
import java.util.Collection;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.ManyToMany;@Entity
@Inheritance
public class Project {@Idprotected int id;protected String name;@ManyToManyprotected Collection<Professor> employees = new ArrayList<Professor>();public int getId() {return id;}public void setId(int projectNo) {this.id = projectNo;}public String getName() {return name;}public void setName(String projectName) {this.name = projectName;}public Collection<Professor> getProfessors() {return employees;}public void addProfessor(Professor employee) {if (!getProfessors().contains(employee)) {getProfessors().add(employee);}if (!employee.getProjects().contains(this)) {employee.getProjects().add(this);}}public String toString() {return getClass().getName().substring(getClass().getName().lastIndexOf(".")+1) + " no: " + getId() + ", name: " + getName();}
}

以下代码来自Address.java

package cn.w3cschool.common;import javax.persistence.Entity;
import javax.persistence.Id;@Entity
public class Address {@Idprivate 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 = id;}public String getStreet() {return street;}public void setStreet(String address) {this.street = address;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getState() {return state;}public void setState(String state) {this.state = state;}public String getZip() {return zip;}public void setZip(String zip) {this.zip = zip;}public String toString() {return "Address id: " + getId() + ", street: " + getStreet() +", city: " + getCity() +", state: " + getState() +", zip: " + getZip();}}

以下代码来自Phone.java

package cn.w3cschool.common;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;@Entity
public class Phone {@Idprivate long id;private String number;private String type;@ManyToOneProfessor employee;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getNumber() {return number;}public void setNumber(String phoneNo) {this.number = phoneNo;}public String getType() {return type;}public void setType(String phoneType) {this.type = phoneType;}public Professor getProfessor() {return employee;}public void setProfessor(Professor employee) {this.employee = employee;}public String toString() {return "Phone id: " + getId() + ", no: " + getNumber() +", type: " + getType();}
}

下载 Query_Count_Where_Subquery.zip

以下是数据库转储。

Table Name: ADDRESSTable Name: DEPARTMENTRow:Column Name: ID,Column Type: INTEGER:Column Value: 1Column Name: NAME,Column Type: VARCHAR:Column Value: DesignTable Name: PHONETable Name: PROFESSORRow:Column Name: ID,Column Type: INTEGER:Column Value: 0Column Name: NAME,Column Type: VARCHAR:Column Value: TOMColumn Name: SALARY,Column Type: BIGINT:Column Value: 1111Column Name: STARTDATE,Column Type: DATE:Column Value: nullColumn Name: ADDRESS_ID,Column Type: INTEGER:Column Value: nullColumn Name: DEPARTMENT_ID,Column Type: INTEGER:Column Value: 1Column Name: MANAGER_ID,Column Type: INTEGER:Column Value: nullTable Name: PROJECTTable Name: PROJECT_PROFESSOR

JPA 查询函数示例

JPA教程 - JPA查询函数示例

JPQL支持的函数表达式如下所示。

函数描述
ABS(number)返回number参数的无符号版本。结果类型与参数类型(整数,浮点型或双精度型)相同。
CONCAT(string1,string2)连接string1和string2。
CURRENT_DATE返回数据库服务器定义的当前日期。
CURRENT_TIME从数据库服务器返回当前时间。
CURRENT_TIMESTAMP返回数据库服务器的当前时间戳。
INDEX(identification variable)返回有序列表中实体的位置。
LENGTH(string)返回字符串参数中的字符数。
LOCATE(string1,string2 [,start])返回string1中string1的位置,可选择从start指示的位置开始。如果找不到字符串,结果为零。
LOWER(string)返回字符串参数的小写形式。
MOD(number1,number2)返回数字参数number1和number2的模数作为整数。
SIZE(collection)返回集合中元素的数量,如果集合为空则返回0。
SQRT(number)返回数字参数的平方根作为double。
SUBSTRING(string, start, end)返回输入字符串的一部分,从由start开始到length字符指示的索引开始。字符串索引从一个开始。
UPPER(string)返回字符串参数的大写形式。
TRIM([[LEADING | TRAILING | BOTH] [char] FROM] string)删除字符串中的前导和/或尾随字符。如果不使用可选的LEADING,TRAILING或BOTH关键字,则删除前导和尾随字符。默认修剪字符是空格字符。

以下代码显示如何在JPQL中使用CONCAT,SUBSTRING和LOCATE字符串函数。

    em.createQuery("UPDATE Phone p SET p.number = "+ "CONCAT("0",SUBSTRING(p.number,"+ "LOCATE(p.number, "-"), 4)), p.type = "Business" ").executeUpdate();

例子

以下代码来自Project.java

package cn.w3cschool.common;import java.util.ArrayList;
import java.util.Collection;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.ManyToMany;@Entity
@Inheritance
public class Project {@Idprotected int id;protected String name;@ManyToManyprotected Collection<Professor> employees = new ArrayList<Professor>();public int getId() {return id;}public void setId(int projectNo) {this.id = projectNo;}public String getName() {return name;}public void setName(String projectName) {this.name = projectName;}public Collection<Professor> getProfessors() {return employees;}public void addProfessor(Professor employee) {if (!getProfessors().contains(employee)) {getProfessors().add(employee);}if (!employee.getProjects().contains(this)) {employee.getProjects().add(this);}}public String toString() {return getClass().getName().substring(getClass().getName().lastIndexOf(".")+1) + " no: " + getId() + ", name: " + getName();}
}

下面的代码来自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();em.createQuery("UPDATE Phone p SET p.number = "+ "CONCAT("0",SUBSTRING(p.number,"+ "LOCATE(p.number, "-"), 4)), p.type = "Business" ").executeUpdate();}private void prepareData() {Professor p = new Professor();p.setId(0);p.setName("TOM");p.setSalary(1111L);Department d = new Department();d.setId(1);d.setName("Design");p.setDepartment(d);d.getProfessors().add(p);Phone phone = new Phone();phone.setId(1);phone.setNumber("111-111-1111");phone.setProfessor(p);em.persist(p);em.persist(phone);em.persist(d);}private static void printResult(Object result) {if (result == null) {System.out.print("NULL");} else if (result instanceof Object[]) {Object[] row = (Object[]) result;System.out.print("[");for (int i = 0; i < row.length; i++) {printResult(row[i]);}System.out.print("]");} else if (result instanceof Long || result instanceof Double|| result instanceof String) {System.out.print(result.getClass().getName() + ": " + result);} else {System.out.print(result);}System.out.println();}@PersistenceContextprivate EntityManager em;
}

以下代码来自Phone.java

package cn.w3cschool.common;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;@Entity
public class Phone {@Idprivate long id;private String number;private String type;@ManyToOneProfessor employee;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getNumber() {return number;}public void setNumber(String phoneNo) {this.number = phoneNo;}public String getType() {return type;}public void setType(String phoneType) {this.type = phoneType;}public Professor getProfessor() {return employee;}public void setProfessor(Professor employee) {this.employee = employee;}public String toString() {return "Phone id: " + getId() + ", no: " + getNumber() +", type: " + getType();}
}

以下代码来自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 {@Idprivate int id;private String name;private long salary;@Temporal(TemporalType.DATE)private Date startDate;@OneToOneprivate Address address;@OneToMany(mappedBy="employee")private Collection<Phone> phones = new ArrayList<Phone>();@ManyToOneprivate Department department;@ManyToOneprivate 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() {return name;}public void setName(String name) {this.name = name;}public long getSalary() {return salary;}public void setSalary(long salary) {this.salary = salary;}public Date getStartDate() {return startDate;}public void setStartDate(Date startDate) {this.startDate = startDate;}public Collection<Phone> getPhones() {return phones;}public void addPhone(Phone phone) {if (!getPhones().contains(phone)) {getPhones().add(phone);if (phone.getProfessor() != null) {phone.getProfessor().getPhones().remove(phone);}phone.setProfessor(this);}}public Department getDepartment() {return department;}public void setDepartment(Department department) {if (this.department != null) {this.department.getProfessors().remove(this);}this.department = department;this.department.getProfessors().add(this);}public Collection<Professor> getDirects() {return directs;}public void addDirect(Professor employee) {if (!getDirects().contains(employee)) {getDirects().add(employee);if (employee.getManager() != null) {employee.getManager().getDirects().remove(employee);}employee.setManager(this);}}public Professor getManager() {return manager;}public void setManager(Professor manager) {this.manager = manager;}public Collection<Project> getProjects() {return projects;}public void addProject(Project project) {if (!getProjects().contains(project)) {getProjects().add(project);}if (!project.getProfessors().contains(this)) {project.getProfessors().add(this);}}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address; }public String toString() {return "Professor " + getId() + ": name: " + getName() +", salary: " + getSalary() +", phones: " + getPhones() +", managerNo: " + ((getManager() == null) ? null : getManager().getId()) +", deptNo: " + ((getDepartment() == null) ? null : getDepartment().getId());}}

以下代码来自Address.java

package cn.w3cschool.common;import javax.persistence.Entity;
import javax.persistence.Id;@Entity
public class Address {@Idprivate 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 = id;}public String getStreet() {return street;}public void setStreet(String address) {this.street = address;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getState() {return state;}public void setState(String state) {this.state = state;}public String getZip() {return zip;}public void setZip(String zip) {this.zip = zip;}public String toString() {return "Address id: " + getId() + ", street: " + getStreet() +", city: " + getCity() +", state: " + getState() +", zip: " + getZip();}}

下面的代码来自Department.java

package cn.w3cschool.common;import java.util.HashSet;
import java.util.Set;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;@Entity
public class Department {@Idprivate int id;private String name;@OneToMany(mappedBy="department")private Set<Professor> employees = new HashSet<Professor>();public int getId() {return id;}public void setId(int deptNo) {this.id = deptNo;}public String getName() {return name;}public void setName(String deptName) {this.name = deptName;}public Set<Professor> getProfessors() {return employees;}public String toString() {return "Department no: " + getId() + ", name: " + getName();}
}

下载 Query_Substring.zip

以下是数据库转储。

Table Name: ADDRESSTable Name: DEPARTMENTRow:Column Name: ID,Column Type: INTEGER:Column Value: 1Column Name: NAME,Column Type: VARCHAR:Column Value: DesignTable Name: PHONERow:Column Name: ID,Column Type: BIGINT:Column Value: 1Column Name: NUMBER,Column Type: VARCHAR:Column Value: 0111Column Name: TYPE,Column Type: VARCHAR:Column Value: BusinessColumn Name: EMPLOYEE_ID,Column Type: INTEGER:Column Value: 0Table Name: PROFESSORRow:Column Name: ID,Column Type: INTEGER:Column Value: 0Column Name: NAME,Column Type: VARCHAR:Column Value: TOMColumn Name: SALARY,Column Type: BIGINT:Column Value: 1111Column Name: STARTDATE,Column Type: DATE:Column Value: nullColumn Name: ADDRESS_ID,Column Type: INTEGER:Column Value: nullColumn Name: DEPARTMENT_ID,Column Type: INTEGER:Column Value: 1Column Name: MANAGER_ID,Column Type: INTEGER:Column Value: nullTable Name: PROJECTTable Name: PROJECT_PROFESSOR


http://www.ppmy.cn/ops/145882.html

相关文章

Mac mini m4安装PD和Crack和关闭SIP

文章目录 说明PD下载地址Crack下载地址PD版本补丁版本说明SIP简介SIP关闭方法启用 SIP&#xff1a;补充说明 说明 参考文章地址 PD下载地址 PD20.0.0 PD20.0.1 PD20.1.0 PD20.1.1 PD20.1.2 PD20.1.3 Crack下载地址 Parallels Desktop Activation Tool PD版本补丁版本…

虚拟机桥接模式

主机Win10,虚拟机xp 1.虚拟机设置中选择桥接模式 2.在虚拟机菜单&#xff1a;编辑>虚拟机网络编辑&#xff0c;点击“更改设置”&#xff0c;可以看到三个网卡&#xff0c;这三个网卡分别对应不同的网络共享模式。桥接模式须使用VMnet0&#xff0c;如果没看到这个网卡&…

一个服务器可以搭建几个网站?搭建一个网站的流程介绍

在数字化时代&#xff0c;网站已经成为企业、组织乃至个人展示形象、推广产品、提 供服务的重要窗口。而服务器作为网站的载体&#xff0c;其性能与配置直接关系到网站的 稳定与高效。那么&#xff0c;一个服务器究竟能搭建几个网站?搭建一个网站的流程又是怎 样的呢?本文将为…

【算法day25】贪心:重叠区间2

题目引用 合并区间单调递增的数字 1. 合并区间 以数组 intervals 表示若干个区间的集合&#xff0c;其中单个区间为 intervals[i] [starti, endi] 。请你合并所有重叠的区间&#xff0c;并返回 一个不重叠的区间数组&#xff0c;该数组需恰好覆盖输入中的所有区间 。 示例 1&…

流架构的读书笔记(2)

流架构的读书笔记&#xff08;2&#xff09; 一、建模工具之一沃德利地图 推测技术的发展,交流和辩论思想的最有力的方法是沃德利地图 沃德利地图的制作步骤 1确定范围和用户需求 2确定满足用户需求所需的组件 3在一条范围从全新到被人们接受的演进轴上评估这些组成 部分的演…

5.npm包

文章目录 [TOC](文章目录) 3.npm与包3.1.包3.2.npm体验在项目中安装包的命令包管理配置文件一次性安装开发项目时安装的包如何从项目中卸载包devDependencies节点的作用解决下载包速度比较慢的问题nrm工具&#xff0c;利用其提供的终端命令&#xff0c;可以快速查看和切换下包的…

mybatis/mybatisplus

一、mybatis 1.什么是 持久化框架。通过XML或注解的方式将实体类和SQL语句进行映射&#xff0c;开发者快速进行CRUD操作。 2.核心组件 (1)SqlSessionFactory 创建SqlSession实例。用于执行SQL操作 。代码 String resource "org/mybatis/example/mybatis-config.xml&q…

【反转链表系列】力扣206,92,25

文章目录 一、前言二、反转链表三、反转链表 II四、K 个一组翻转链表 一、前言 本文涉及的力扣题目&#xff0c;涉及简单、中等、困难三个难度 力扣206.反转链表力扣92.反转链表 II力扣25.K 个一组翻转链表 反转链表这类型的题目&#xff0c;折磨了挺久&#xff0c;经常弄懂了一…