JPA Learning - Lesson 14, Related Query

About Inner and Outer Connections

The inner
join is the inner join, the two parties are related to each other, not which one is the main one. Must be associated with, if not associated with data, it will be discarded. Belongs to the smallest set of associations.
Outer join
Outer join is divided into left (outer) join left (out) join and right (outer) join right (out) join, usually "outer" can be omitted.
left join

select * from a left join b on a.xx=b.xx;

The left join is based on the left table a as the basis to associate the right table b. Regardless of whether it is associated with data, the final result is that the a table is all checked out, and the b table is attached to the a table as a connected identity. So this query result set is greater than or equal to the capacity of table a.

Right join
and left join are based on the b table instead.

The above briefly talks about the syntax knowledge of some relational queries in sql. Let's take a look at the application of relational queries in JPQL.

Use association relationship query :
It is to use many-to-one to create foreign key queries, specific previous blogs have, slightly...
Query:

String jpql ="select e FROM Employee e WHERE e.id = ?";
Query query = em.createQuery(jpql);
query.setParameter(1, 1);
Employee emp = (Employee)query.getSingleResult();
System.out.println(emp);

Query situation:
write picture description here
two selects, no relational query is used

Here we can use left outer join to associate the query and call the query only once

String jpql = "select e FROM Employee e left outer join fetch e.department WHERE e.id = ?";

The above statement also uses fetch without using the outer join query. In fact, left join and inner join are mainly used here to cooperate with fetch, one fetch, because the default is generally lazy loading

Query situation:
write picture description here
You can see that the left outer join is used to query two tables at a time.
To be able to execute the above successfully, the association relationship must be configured. The many-to-one relationship I use here @ManyToOne

Can I still use a relational query without configuring the relational relationship? Of course it can.
The multi-party entity class with the association relationship configured before the transformation:

package com.ssj.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="employee")
public class Employee {

    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Id
    @Column(name="ID")
    private Integer Id;

    @Column(name="EMP_NAME")
    private String empName;

    @Column(name="EMP_BIRTH")
    @Temporal(TemporalType.DATE)
    private Date empBirth;

    @Column(name="DEPT_ID")
    private Integer deptId;

    public Integer getId() {
        return Id;
    }
    public void setId(Integer id) {
        Id = id;
    }
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public Date getEmpBirth() {
        return empBirth;
    }
    public void setEmpBirth(Date empBirth) {
        this.empBirth = empBirth;
    }
    public Integer getDeptId() {
        return deptId;
    }
    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }
    @Override
    public String toString() {
        return "Employee [Id=" + Id + ", empName=" + empName + ", empBirth="
                + empBirth + ", deptId=" + deptId + "]";
    }
}

Delete the configuration of the association relationship and directly create a column of DEPT_ID to store data related to another table. There is no foreign key created here.

String jpql = "select u,d FROM Employee u left join Department d on u.deptId=d.id";

Use left join on query, query situation:
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325974934&siteId=291194637