枣庄网站seo,深圳数码网站建设,杭州做公司官网的公司,阿里巴巴做实商网站的条件JPA 查询简介示例
JPA教程 - JPA查询简介示例 最简单的JPQL查询选择单个实体类型的所有实例。
考虑下面的查询:
SELECT e
FROM Employee eJPQL尽可能使用SQL语法。
SQL查询从表中选择。JPQL从应用程序域模型的实体中选择。
语法
选择查询的整体形式如下:
SELECT sel…JPA 查询简介示例
JPA教程 - JPA查询简介示例 最简单的JPQL查询选择单个实体类型的所有实例。
考虑下面的查询:
SELECT e
FROM Employee eJPQL尽可能使用SQL语法。
SQL查询从表中选择。JPQL从应用程序域模型的实体中选择。
语法
选择查询的整体形式如下:
SELECT select_expression
FROM from_clause
[WHERE 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();ListProfessor l em.createQuery(SELECT e FROM Professor e).getResultList();for(Professor p:l){System.out.println(p);}}private void prepareData(){Professor p new Professor();p.setId(0);p.setName(TOM);em.persist(p);}PersistenceContextprivate EntityManager em;
}以下代码来自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(mappedByemployee)private CollectionPhone phones new ArrayListPhone();ManyToOneprivate Department department;ManyToOneprivate Professor manager;OneToMany(mappedBymanager)private CollectionProfessor directs new ArrayListProfessor();ManyToMany(mappedByemployees)private CollectionProject projects new ArrayListProject();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 CollectionPhone 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 CollectionProfessor 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 CollectionProject 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_Simple.zip
上面的代码生成以下结果。 以下是数据库转储。
Table Name: ADDRESSTable Name: DEPARTMENTTable 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: 0Column 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: nullColumn Name: MANAGER_ID,Column Type: INTEGER:Column Value: nullTable Name: PROJECTTable Name: PROJECT_PROFESSORJPA 查询选择两个实体示例
JPA教程 - JPA查询选择两个实体示例 以下JPQL从两个实体中选择。
List l em.createQuery(SELECT d, m FROM Department d, Professor m WHERE d m.department).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(mappedByemployee)private CollectionPhone phones new ArrayListPhone();ManyToOneprivate Department department;ManyToOneprivate Professor manager;OneToMany(mappedBymanager)private CollectionProfessor directs new ArrayListProfessor();ManyToMany(mappedByemployees)private CollectionProject projects new ArrayListProject();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 CollectionPhone 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 CollectionProfessor 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 CollectionProject 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();}}下面的代码来自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, m FROM Department d, Professor m WHERE d m.department).getResultList();for(Object p:l){System.out.println(p);}}private void prepareData(){Professor p new Professor();p.setId(0);p.setName(TOM);Department d new Department();d.setId(1);d.setName(Design);p.setDepartment(d);d.getProfessors().add(p);em.persist(p);em.persist(d);}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();}
}以下代码来自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 CollectionProfessor employees new ArrayListProfessor();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 CollectionProfessor 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();}
}下面的代码来自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(mappedBydepartment)private SetProfessor employees new HashSetProfessor();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 SetProfessor getProfessors() {return employees;}public String toString() {return Department no: getId() , name: getName();}
}下载 Query_Select_Two_Entities.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: 0Column 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_PROFESSORJPA 查询选择两个属性示例 JPA教程 - JPA查询选择两个属性示例 从别名开始我们可以使用点.运算符浏览实体关系。
以下JPQL从一个实体中选择两个属性。 List l em.createQuery(SELECT e.name, e.salary FROM Professor e)例子
下面的代码来自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.name, 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);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 CollectionProfessor employees new ArrayListProfessor();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 CollectionProfessor 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();}
}以下代码来自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(mappedByemployee)private CollectionPhone phones new ArrayListPhone();ManyToOneprivate Department department;ManyToOneprivate Professor manager;OneToMany(mappedBymanager)private CollectionProfessor directs new ArrayListProfessor();ManyToMany(mappedByemployees)private CollectionProject projects new ArrayListProject();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 CollectionPhone 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 CollectionProfessor 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 CollectionProject 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(mappedBydepartment)private SetProfessor employees new HashSetProfessor();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 SetProfessor getProfessors() {return employees;}public String toString() {return Department no: getId() , name: getName();}
}下载 Query_Select_Two_Properties.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: 0Column 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_PROFESSORJPA 查询命名参数
JPA教程 - JPA查询命名参数示例 JPQL支持两种类型的参数绑定语法。
第一种是位置绑定其中参数在查询字符串中通过问号后面跟随参数号来指示。
执行查询时开发人员指定应替换的参数编号。
SELECT e
FROM Employee e
WHERE e.department ?1 ANDe.salary ?2命名参数在查询字符串中用冒号后跟参数名称指示。
当执行查询时开发人员指定应该替换的参数名称。
这种类型的参数允许更具描述性的参数说明符。
SELECT e
FROM Employee e
WHERE e.department :dept ANDe.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 {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();}}下面的代码来自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();Department d new Department();d.setId(1);d.setName(Design);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();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(mappedBydepartment)private SetProfessor employees new HashSetProfessor();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 SetProfessor 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 CollectionProfessor employees new ArrayListProfessor();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 CollectionProfessor 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(mappedByemployee)private CollectionPhone phones new ArrayListPhone();ManyToOneprivate Department department;ManyToOneprivate Professor manager;OneToMany(mappedBymanager)private CollectionProfessor directs new ArrayListProfessor();ManyToMany(mappedByemployees)private CollectionProject projects new ArrayListProject();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 CollectionPhone 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 CollectionProfessor 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 CollectionProject 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_Named_Parameter.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_PROFESSORJPA 查询实体参数值示例
JPA教程 - JPA 查询实体参数值示例 以下代码显示如何将实体值作为JPQL的参数传递。 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();例子
下面的代码来自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(mappedBydepartment)private SetProfessor employees new HashSetProfessor();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 SetProfessor 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();}
}以下代码来自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();}}下面的代码来自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();Department d new Department();d.setId(1);d.setName(Design);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();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;
}以下代码来自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(mappedByemployee)private CollectionPhone phones new ArrayListPhone();ManyToOneprivate Department department;ManyToOneprivate Professor manager;OneToMany(mappedBymanager)private CollectionProfessor directs new ArrayListProfessor();ManyToMany(mappedByemployees)private CollectionProject projects new ArrayListProject();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 CollectionPhone 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 CollectionProfessor 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 CollectionProject 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 CollectionProfessor employees new ArrayListProfessor();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 CollectionProfessor 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_Entity_Parameter_Value.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_PROFESSORJPA 查询编号索引参数示例
JPA教程 - JPA查询编号索引参数示例 以下代码显示了如何在JPQL中使用数字索引参数。
Collection expired em.createQuery(SELECT u FROM Professor u WHERE u.id ?1).setParameter(1, 1).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(mappedBydepartment)private SetProfessor employees new HashSetProfessor();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 SetProfessor getProfessors() {return employees;}public String toString() {return Department no: getId() , name: getName();}
}下面的代码来自PersonDaoImpl.java。
package cn.w3cschool.common;import java.util.Collection;
import java.util.Iterator;import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;import org.springframework.transaction.annotation.Transactional;Transactional
public class PersonDaoImpl {public void test() {prepareData();Collection expired em.createQuery(SELECT u FROM Professor u WHERE u.id ?1).setParameter(1,1).getResultList();for (Iterator i expired.iterator(); i.hasNext();) {Professor u (Professor) i.next();printResult(u);}}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;
}以下代码来自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.NamedNativeQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;Entity
Table(name EMP)
public class Professor {Idprivate int id;private String name;private long salary;Temporal(TemporalType.DATE)private Date startDate;OneToOneprivate Address address;OneToMany(mappedByemployee)private CollectionPhone phones new ArrayListPhone();ManyToOneprivate Department department;ManyToOneprivate Professor manager;OneToMany(mappedBymanager)private CollectionProfessor directs new ArrayListProfessor();ManyToMany(mappedByemployees)private CollectionProject projects new ArrayListProject();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 CollectionPhone 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 CollectionProfessor 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 CollectionProject 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 CollectionProfessor employees new ArrayListProfessor();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 CollectionProfessor 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_Number_Indexed_Parameters.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: EMPRow: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: 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: PROJECTTable Name: PROJECT_EMPJPA 查询日期参数示例
JPA教程 - JPA查询日期参数示例 我们可以在查询中使用日期类型值。
以下代码使用EntityManager创建具有两个参数的查询。
然后它传递两个日期类型值。
em.createQuery(SELECT e FROM Professor e WHERE e.startDate BETWEEN :start AND :end).setParameter(start, new Date(), TemporalType.DATE).setParameter(end, new Date(), TemporalType.DATE).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.NamedNativeQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;Entity
Table(name EMP)
public class Professor {Idprivate int id;private String name;private long salary;Temporal(TemporalType.DATE)private Date startDate;OneToOneprivate Address address;OneToMany(mappedByemployee)private CollectionPhone phones new ArrayListPhone();ManyToOneprivate Department department;ManyToOneprivate Professor manager;OneToMany(mappedBymanager)private CollectionProfessor directs new ArrayListProfessor();ManyToMany(mappedByemployees)private CollectionProject projects new ArrayListProject();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 CollectionPhone 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 CollectionProfessor 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 CollectionProject 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 CollectionProfessor employees new ArrayListProfessor();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 CollectionProfessor 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();}
}下面的代码来自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(mappedBydepartment)private SetProfessor employees new HashSetProfessor();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 SetProfessor 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();}
}以下代码来自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();}}下面的代码来自PersonDaoImpl.java。
package cn.w3cschool.common;import java.util.Collection;
import java.util.Date;
import java.util.Iterator;import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TemporalType;import org.springframework.transaction.annotation.Transactional;Transactional
public class PersonDaoImpl {public void test() {prepareData();CollectionProfessor c em.createQuery(SELECT e FROM Professor e WHERE e.startDate BETWEEN :start AND :end).setParameter(start, new Date(), TemporalType.DATE).setParameter(end, new Date(), TemporalType.DATE).getResultList();for (Iterator i c.iterator(); i.hasNext();) {Professor u (Professor) i.next();printResult(u);}}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_Date_Parameters.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: EMPRow: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: 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: PROJECTTable Name: PROJECT_EMP