我尝试在spring Boot 应用程序中应用inner join,以从两个表(即vehicle和customer)中获取所需的列。
我有两个实体,即车辆和客户
package com.eichers.vehicleapi.entity;
import jakarta.persistence.*;
@Entity
@Table(name="vehicle")
public class Vehicle {
//define fields
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "chassis")
private int chassis;
@Column(name = "regno")
private String regno;
@Column(name = "customerconsent")
private String customerconsent;
//define constructors
public Vehicle(String regno, String customerconsent) {
this.regno = regno;
this.customerconsent = customerconsent;
}
public Vehicle() {
}
//define getter and setter
public int getChassis() {
return chassis;
}
public void setChassis(int chassis) {
this.chassis = chassis;
}
public String getRegno() {
return regno;
}
public void setRegno(String regno) {
this.regno = regno;
}
public String getCustomerconsent() {
return customerconsent;
}
public void setCustomerconsent(String customerconsent) {
this.customerconsent = customerconsent;
}
//define toString method
@Override
public String toString() {
return "Vehicle{" +
"chassis=" + chassis +
", regno='" + regno + '\'' +
", customerconsent='" + customerconsent + '\'' +
'}';
}
}
客户实体
package com.eichers.vehicleapi.entity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "customer")
public class Customer {
//define fields
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "customer_id")
private Integer customerId;
@Column(name = "type")
private String type;
@Column(name = "first_name")
private String firstname;
@Column(name = "last_name")
private String lastname;
@OneToOne
@JoinColumn(name="chassis_id")
private Vehicle vehicle;
//constructor
public Customer(String type, String firstname, String lastname, Vehicle vehicle) {
this.type = type;
this.firstname = firstname;
this.lastname = lastname;
this.vehicle = vehicle;
}
}
细节类
package com.eichers.vehicleapi.entity;
import jakarta.persistence.Column;
public class Details {
@Column(name="chassis")
private int chassis;
@Column(name="first_name")
private String firstname;
@Column(name="last_name")
private String lastname;
@Column(name="regno")
private String regno;
@Column(name="customerconsent")
private String customerconsent;
// Details class for the final table which I expect
public Details() {
}
public Details(int chassis, String firstname, String lastname, String regno, String customerconsent) {
this.chassis = chassis;
this.firstname = firstname;
this.lastname = lastname;
this.regno = regno;
this.customerconsent = customerconsent;
}
public int getChassis() {
return chassis;
}
public void setChassis(int chassis) {
this.chassis = chassis;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getRegno() {
return regno;
}
public void setRegno(String regno) {
this.regno = regno;
}
public String getCustomerconsent() {
return customerconsent;
}
public void setCustomerconsent(String customerconsent) {
this.customerconsent = customerconsent;
}
}
我为join编写的查询:
@Override
public List<Details> getDetails() {
TypedQuery<Details> theQuery = entityManager.createQuery("SELECT Vehicle.chassis, Customer.firstname, Customer.lastname, Vehicle.regno, Vehicle.customerconsent FROM Customer JOIN Vehicle ON Customer.chassis_id = vehicle.chassis",Details.class);
//get the list
List<Details> details = theQuery.getResultList();
return details;
}
我得到的错误是
org.hibernate.query.SemanticException: Could not interpret path expression 'Customer.chassis_id'
在上面我不知道该怎么处理Customer.chassis*_id作为chassis_id是数据库中属性的名称。*
我应该如何在spring Boot 查询中正确使用外键?因为当我在PostgreSQL中运行它时,它会给出结果。
SELECT vehicle.chassis, customer.first_name, customer.last_name, vehicle.regno, vehicle.customerconsent FROM vehicle JOIN customer ON vehicle.chassis = customer.chassis_id;
chassis | first_name | last_name | regno | customerconsent
---------+------------+-----------+------------+----------------------------------
2 | Kunal | Ganjawala | 122yamaha | it is great driving yamaha
3 | Mohit | Chauhan | 122hero | it is great driving hero
3000 | Sonu | Nigam | 122hero | it is great driving hero
3001 | Arijit | Singh | 129jawa | it is great driving jawa
3003 | Armaan | Mallik | 1230pulsor | pulsor makes me feel like flying
(5 rows)
以上是我所期望的,但它没有发生。
1条答案
按热度按时间yhqotfr81#
您正在使用JPQL,并且在连接中使用实际的列名而不是属性名。JPQL使用属性名而不是列名。
更新您的查询如下。