您的位置:首页 > 汽车 > 时评 > 吉林省人民政府征兵办公室_网页建设培训机构_网站优化软件费用_信息流优化师工作总结

吉林省人民政府征兵办公室_网页建设培训机构_网站优化软件费用_信息流优化师工作总结

2024/12/26 21:47:12 来源:https://blog.csdn.net/m0_69824302/article/details/144703169  浏览:    关键词:吉林省人民政府征兵办公室_网页建设培训机构_网站优化软件费用_信息流优化师工作总结
吉林省人民政府征兵办公室_网页建设培训机构_网站优化软件费用_信息流优化师工作总结

JPA 查询All示例

JPA教程 - JPA查询All示例

我们可以在JPQL中使用带有子查询的ALL运算符。

List l = em.createQuery("SELECT e FROM Professor e WHERE e.salary < "+ "ALL (SELECT d.salary FROM e.directs d)").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 e.salary < "+ "ALL (SELECT d.salary FROM e.directs d)").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;
}

下面的代码来自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();}
}

以下代码来自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();}
}

以下代码来自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_All.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 查询ANY示例

JPA教程 - JPA查询ANY示例

以下代码显示了如何在JPQL中使用ANY运算符。

List l = em.createQuery("SELECT e FROM Professor e WHERE e.department = ANY (SELECT DISTINCT d FROM Department d)").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 e.department = ANY (SELECT DISTINCT d FROM Department d)").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;
}

以下代码来自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());}}

以下代码来自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();}
}

下面的代码来自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();}
}

以下代码来自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();}
}

下载 Query_ANY.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 查询IN示例

JPA教程 - JPA查询IN示例

IN表达式可以检查单值路径表达式是否是集合的成员。

集合可以内联定义为一组字面值,或者可以从子查询派生。

以下代码显示了如何在JPQL中使用IN运算符。

List l = em.createQuery("SELECT DISTINCT p FROM Professor e, IN(e.phones) p").getResultList();

以下代码显示如何使用NOT IN运算符

List l = em.createQuery("SELECT e FROM Professor e WHERE e.address.state NOT IN ("NY", "CA")").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 DISTINCT p FROM Professor e, IN(e.phones) p").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;
}

以下代码来自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();}
}

下面的代码来自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();}
}

以下代码来自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();}
}

以下代码来自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());}}

下载 Query_IN.zip
下载 Query_NOT_IN.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 查询IN一对多示例

JPA教程 - JPA查询IN一对多示例

以下JPQL显示如何在一对多映射中使用IN运算符。

executeAndPrintQuery("SELECT DISTINCT p FROM Employee e, IN(e.directs) p");

例子

下面的代码来自Department.java。

package cn.w3cschool.common;import java.util.ArrayList;
import java.util.Collection;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 Collection<Employee> employees;public Department() {employees = new ArrayList<Employee>();}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}public void setEmployees(Collection<Employee> employees) {this.employees = employees;}public int getId() {return id;}public String getName() {return name;}public Collection<Employee> getEmployees() {return employees;}public String toString() {return "Department no: " + getId() + ", name: " + getName();}
}

以下代码来自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.ManyToMany;@Entity
public class Project {@Idprotected int id;protected String name;@ManyToMany(mappedBy="projects")private Collection<Employee> employees;public Project() {employees = new ArrayList<Employee>();}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}public void setEmployees(Collection<Employee> employees) {this.employees = employees;}public int getId() {return id;}public String getName() {return name;}public Collection<Employee> getEmployees() {return employees;}public String toString() {return "Project id: " + getId() + ", name: " + getName();}
}

以下代码来自Employee.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.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Employee {@Idprivate int id;private String name;private long salary;@Temporal(TemporalType.DATE)private Date startDate;@ManyToOneprivate Employee manager;@OneToMany(mappedBy="manager")private Collection<Employee> directs;@ManyToOneprivate Department department;@ManyToMany private Collection<Project> projects;public Employee() {projects = new ArrayList<Project>();directs = new ArrayList<Employee>();}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}public void setSalary(long salary) {this.salary = salary;}public void setStartDate(Date startDate) {this.startDate = startDate;}public void setManager(Employee manager) {this.manager = manager;}public void setDirects(Collection<Employee> directs) {this.directs = directs;}public void setDepartment(Department department) {this.department = department;}public void setProjects(Collection<Project> projects) {this.projects = projects;}public int getId() {return id;}public String getName() {return name;}public long getSalary() {return salary;}public Date getStartDate() {return startDate;}public Department getDepartment() {return department;}public Collection<Employee> getDirects() {return directs;}public Employee getManager() {return manager;}public Collection<Project> getProjects() {return projects;}public String toString() {return "Employee " + getId() + ": name: " + getName() +", salary: " + getSalary() +", dept: " + ((getDepartment() == null) ? null : getDepartment().getName());}
}

下面的代码来自PersonDaoImpl.java。

package cn.w3cschool.common;import java.util.Arrays;
import java.util.Date;
import java.util.List;import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;import org.springframework.transaction.annotation.Transactional;@Transactional
public class PersonDaoImpl {public void test() {Employee emp = new Employee();emp.setName("Tom");emp.setSalary(123);emp.setStartDate(new Date());emp.setId(1);Project pro = new Project();pro.setName("Design");pro.getEmployees().add(emp);Department dept = new Department();dept.setName("Dept");dept.getEmployees().add(emp);emp.setDepartment(dept);emp.getProjects().add(pro);em.persist(dept);em.persist(pro);em.persist(emp);executeAndPrintQuery("SELECT DISTINCT p FROM Employee e, IN(e.directs) p");}private void executeAndPrintQuery(String queryString) {try {Query query = em.createQuery(queryString);printQueryResult(queryString, query.getResultList());} finally {em.close();}
}private void printQueryResult(String queryString, List result) {System.out.println("EJB QL: " + queryString+"\n");System.out.println("Result:\n");if (result.isEmpty()) {System.out.println("No results Found\n");} else {for (Object o : result) {System.out.println(resultAsString(o));}}}private String resultAsString(Object o) {if (o instanceof Object[]) {return Arrays.asList((Object[])o).toString();} else {return String.valueOf(o);}
}@PersistenceContextprivate EntityManager em;
}

下载 Query_In_OneToMany.zip

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

以下是数据库转储。

Table Name: DEPARTMENTRow:Column Name: ID,Column Type: INTEGER:Column Value: 0Column Name: NAME,Column Type: VARCHAR:Column Value: DeptTable Name: EMPLOYEERow:Column Name: ID,Column Type: INTEGER:Column Value: 1Column Name: NAME,Column Type: VARCHAR:Column Value: TomColumn Name: SALARY,Column Type: BIGINT:Column Value: 123Column Name: STARTDATE,Column Type: DATE:Column Value: 2014-12-29Column Name: DEPARTMENT_ID,Column Type: INTEGER:Column Value: 0Column Name: MANAGER_ID,Column Type: INTEGER:Column Value: nullTable Name: EMPLOYEE_PROJECTRow:Column Name: EMPLOYEES_ID,Column Type: INTEGER:Column Value: 1Column Name: PROJECTS_ID,Column Type: INTEGER:Column Value: 0Table Name: PROJECTRow:Column Name: ID,Column Type: INTEGER:Column Value: 0Column Name: NAME,Column Type: VARCHAR:Column Value: Design

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com