广州北大青鸟计算机职业培训学校
互联网技术培训、软件技术培训、大数据培训、云计算培训、数据分析培训信息网
当前位置:网站首页 > 软件教程 > Java技术 > 正文

惠州分享Java中的JPA命名查询参数类型实例_北大青鸟IT学校

作者:邓华发布时间:2021-05-09分类:Java技术浏览:848


导读:Java是现在非常主流的编程语言之一,很多人想转行学习Java。那么下面惠州北大青鸟老师给大家分享Java中的JPA命名查询参数类型实例,希望对大家的学习有帮助。

Java是现在非常主流的编程语言之一,很多人想转行学习Java。那么下面惠州北大青鸟老师给大家分享Java中的JPA命名查询参数类型实例,希望对大家的学习有帮助。

实例

以下代码来自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
@NamedQueries({
 @NamedQuery(name="findEmployeesAboveSal",
             query="SELECT e " +                    "FROM Employee e " +                    "WHERE e.department = :dept AND " +                    "      e.salary > :sal"),
 @NamedQuery(name="findHighestPaidByDepartment",
             query="SELECT e " +                    "FROM Employee e " +                    "WHERE e.department = :dept AND " +                    "      e.salary = (SELECT MAX(e2.salary) " +                    "                  FROM Employee e2 " +                    "                  WHERE e2.department = :dept)")
})public class Employee {
   @Id    private int id;    private String name;    private long salary;
   @Temporal(TemporalType.DATE)    private Date startDate;
   
   @ManyToOne    private Employee manager;
   
   @OneToMany(mappedBy="manager")    private Collection<Employee> directs;

   @ManyToOne    private 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());
   }
}

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

@Entitypublic class Project {
   @Id    protected 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();
   }
}

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

@Entitypublic class Department {
   @Id    private 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();
   }
}

下面的代码来自PersonDaoImpl.java。

package cn.w3cschool.common;

import java.util.Collection;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TemporalType;

import org.springframework.transaction.annotation.Transactional;

@Transactionalpublic 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);

   String deptName = "Dept";
   String empName = "Tom";


 }  public Collection<Employee> findEmployeesHiredDuringPeriod(Date start, Date end) {
     return (Collection<Employee>)
         em.createQuery("SELECT e " +                         "FROM Employee e " +                         "WHERE e.startDate BETWEEN :start AND :end")
           .setParameter("start", start, TemporalType.DATE)
           .setParameter("end", end, TemporalType.DATE)
           .getResultList();
 }  public Employee findHighestPaidByDepartment(Department dept) {      try {
         return (Employee) em.createNamedQuery("findHighestPaidByDepartment")
                             .setParameter("dept", dept)
                             .getSingleResult();
     } catch (NoResultException e) {
         return null;
     }
 }  public Collection<Employee> findAllEmployees() {
     return (Collection<Employee>) em.createQuery(              "SELECT e FROM Employee e").getResultList();
 }
 @PersistenceContext  private EntityManager em;
}

以下是数据库转储。

Table Name: DEPARTMENT
Row:
   Column Name: ID,
   Column Type: INTEGER:
   Column Value: 0

   Column Name: NAME,
   Column Type: VARCHAR:
   Column Value: Dept





Table Name: EMPLOYEE
Row:
   Column Name: ID,
   Column Type: INTEGER:
   Column Value: 1

   Column Name: NAME,
   Column Type: VARCHAR:
   Column Value: Tom

   Column Name: SALARY,
   Column Type: BIGINT:
   Column Value: 123

   Column Name: STARTDATE,
   Column Type: DATE:
   Column Value: 2014-12-29

   Column Name: DEPARTMENT_ID,
   Column Type: INTEGER:
   Column Value: 0

   Column Name: MANAGER_ID,
   Column Type: INTEGER:
   Column Value: null





Table Name: EMPLOYEE_PROJECT
Row:
   Column Name: EMPLOYEES_ID,
   Column Type: INTEGER:
   Column Value: 1

   Column Name: PROJECTS_ID,
   Column Type: INTEGER:
   Column Value: 0





Table Name: PROJECT
Row:
   Column Name: ID,
   Column Type: INTEGER:
   Column Value: 0

   Column Name: NAME,
   Column Type: VARCHAR:
   Column Value: Design

想了解更多关于Java的资讯,可以来惠州北大青鸟新方舟校区了解一下。

Java14.png

Java

标签:惠州计算机JAVA软件开发惠州计算机Java软件开发惠州计算机JAVA培训惠州计算机JAVA软件开发学校惠州计算机Java软件开发培训JAVAJava软件开发北大青鸟IT计算机学校北大青鸟IT软件学校北大青鸟IT学校


Java技术排行
标签列表
网站分类
文章归档
最近发表