加入JPA查询 – Spring Boot

huangapple go评论76阅读模式
英文:

Join in JPA query - spring boot

问题

**两个表**

bookings 和 locations

需要使用连接操作(joins)通过 locationid(外键)将两个表连接起来。

**原生查询**

SELECT l.location_name,
b.bookingid,
b.email,
b.vehicle_type,
b.vehicle_no
FROM locations l RIGHT JOIN bookings b
ON b.locationid = l.locationid;

**Bookings** 表实体

@Entity
public class Bookings {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer bookingid;
private String email;
private String vehicle_type;
private String vehicle_no;
private String date;
private String time;
private Integer duration;
private String cost = "0";
private Integer locationid;
private String slotid;
private Integer paid = 0;
}


**Locations** 表实体

@Entity
public class Locations {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer locationid;
private String location_name;
private Integer slots = 0;
private String area;
}


**预期输出**

+---------------+-----------+------------------+---------------+--------------+
| location_name | bookingid | email | vehicle_no | vehicle_type |
+---------------+-----------+------------------+---------------+--------------+
| Pothys | 22 | test05@gmail.com | TN-01-AA-123 | Four Wheeler |
| Brook Fields | 25 | test05@gmail.com | TN-01-AA-1111 | Two Wheeler |
+---------------+-----------+------------------+---------------+--------------+


如何在 Spring 中实现,
有人能帮帮我...
谢谢。
英文:

Two tables

bookings and locations

Need to join two tables with locationid (foreign key) using joins

Native Query

SELECT l.location_name, 
b.bookingid, 
b.email, 
b.vehicle_type, 
b.vehicle_no 
FROM locations l RIGHT JOIN bookings b 
ON b.locationid = l.locationid;

Bookings Table Entity

@Entity
public class Bookings {
    @Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer bookingid;
	private String email;
	private String vehicle_type;
	private String  vehicle_no;
	private String date;
	private String time;
	private Integer duration;
	private String cost = "0";
	private Integer locationid;
	private String slotid;
	private Integer paid = 0;
} 

Locations Table Entity

@Entity
public class Locations {
    @Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer locationid;
	private String location_name;
	private Integer slots = 0;
	private String area;	
}

Expected Output

+---------------+-----------+------------------+---------------+--------------+
| location_name | bookingid |      email       |  vehicle_no   | vehicle_type |
+--------------}-+-----------+------------------+---------------+--------------+
| Pothys        |        22 | test05@gmail.com | TN-01-AA-123  | Four Wheeler |
| Brook Fields  |        25 | test05@gmail.com | TN-01-AA-1111 | Two Wheeler  |
+---------------+-----------+------------------+---------------+--------------+

How to implement in spring,
Some one help me out...
Thank you.

答案1

得分: 0

首先,您必须在实体中定义关系,如下所示:

在预订中定义多对一关系

@Entity
public class Bookings {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer bookingid;
    private String email;
    private String vehicle_type;
    private String vehicle_no;
    private String date;
    private String time;
    private Integer duration;
    private String cost = "0";

    // 将此处更改为
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="locationid")
    private Locations location;

    private String slotid;
    private Integer paid = 0;
}

在位置中定义双向关系

@Entity
public class Locations {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer locationid;
    private String location_name;
    private Integer slots = 0;
    private String area;

    // 添加此部分
    // 你可以使用 Set 而不是 List
    @OneToMany(mappedBy="location",fetch=FetchType.LAZY)
    private List<Bookings> bookings;
}

一旦实体被建模,我们继续进行查询,我不知道您使用的是什么实现,但在此示例中,我将使用 Criteria API:

由于我们不能实现右连接,我们将用内连接替换,它将返回相同的结果。

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<YourPojo> cq = cb.createQuery(YourPojo.class);

Root<Locations> rootLocations = cq.from(Location.class);
Join<Locations, Bookings> joinBookings = rootLocations.join(Locations_.bookings, JoinType.INNER);
// 如果您不使用元模型,请将 Locations_.bookings 替换为 rootLocations.get("bookings")

cq.multiselect(
    rootLocations.get(Locations_.location_name),
    joinBookings.get(Bookings_.bookingid),
    joinBookings.get(Bookings_.email),
    joinBookings.get(Bookings_.vehicle_type),
    joinBookings.get(Bookings_.vehicle_no)
);
// 如果您不使用元模型,请像在连接中一样更改它

List<YourPojo> results = entityManager.createQuery(cq).getResultList();

您将需要一个用于获取数据的构造函数,该构造函数具有与 criteria multiselect 相同的参数,具有相同的顺序和相同的类型

public YourPojo(String location_name, Integer bookingid, String email, String vehicle_type, String vehicle_no){
    [...]
}
英文:

First, you must define the relationships in your entity, as follows:

Define relation many to one in Bookings

@Entity
public class Bookings {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer bookingid;
    private String email;
    private String vehicle_type;
    private String  vehicle_no;
    private String date;
    private String time;
    private Integer duration;
    private String cost = &quot;0&quot;;

    // change this
    @ManyToOne(fetch = FetchType.LAZY)
 	@JoinColumn(name=&quot;locationid&quot;)
    private Locations location;

    private String slotid;
    private Integer paid = 0;
} 

Define the bidirectional relationship in locations

@Entity
public class Locations {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer locationid;
    private String location_name;
    private Integer slots = 0;
    private String area;    

    // add this
    // you can use Set instead of List
    @OneToMany(mappedBy=&quot;location&quot;,fetch=FetchType.LAZY)
    private List&lt;Bookings&gt; bookings;
}

Once the entities are modeled, we proceed to make the query, I don't know what implementation you use, in this example I am going to do it with Criteria API:

As we cannot implement right join, we will substitute for inner join which will return the same result.

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery&lt;YourPojo&gt; cq = cb.createQuery(YourPojo.class);

Root&lt;Locations&gt; rootLocations = cq.from(Location.class);
Join&lt;Locations,Bookings&gt; joinBookings = rootLocations.join(Locations_.bookings,JoinType.INNER);
// If you don&#39;t use metamodel change Locations_.bookings for rootLocations.get(&quot;bookings&quot;)

cq.multiselect(
    rootLocations.get(Locations_.location_name),
    joinBookings.get(Bookings_.bookingid),
    joinBookings.get(Bookings_.email),
    joinBookings.get(Bookings_.vehicle_type),
    joinBookings.get(Bookings_.vehicle_no)
);
//If you don&#39;t use metamodel change it as in join

List&lt;YourPojo&gt; results = entityManager.createQuery(cq).getResultList();

You will need a well to obtain the data, which has a constructor with the same parameters as the criteria multiselect, with the same order and the same type

public YourPojo(String location_name,Integer bookingid,String email, String vehicle_type, String vehicle_no){
    [...]
}

huangapple
  • 本文由 发表于 2020年8月21日 13:54:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63517236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定