gpt4 book ai didi

java - 如何使用Spring+Hibernate查询join表

转载 作者:行者123 更新时间:2023-11-29 02:55:10 28 4
gpt4 key购买 nike

我使用 Spring MVC + Hibernate + MySql 在带有 Intellij 编辑器的 Tomcat 上运行

我想像这样像MySql一样查询HQL

SELECT * from Customer c LEFT JOIN Person p ON p.idPerson = c.idCustomer

它不工作它是这样的返回错误

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [from com.springapp.mvc.model.Customer c LEFT JOIN Person p ON p.idPerson = c.idCustomer]

这是我的数据库

    CREATE TABLE `Customer` (
`idCustomer` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idCustomer`),
CONSTRAINT `customer to person` FOREIGN KEY (`idCustomer`) REFERENCES `Person` (`idPerson`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `Person` (
`idPerson` int(11) NOT NULL AUTO_INCREMENT,
`country` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idPerson`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

关系是从 customer.idCustomer 到 person.idPerson 的 OneToOne

这是我的代码

模型.客户.java

@Entity
@Table(name = "Customer")
public class Customer {
private int idCustomer;
private String name;

@Id
@Column(name = "idCustomer")
public int getIdCustomer() {
return idCustomer;
}

public void setIdCustomer(int idCustomer) {
this.idCustomer = idCustomer;
}

@Basic
@Column(name = "name")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Customer customer = (Customer) o;

if (idCustomer != customer.idCustomer) return false;
if (name != null ? !name.equals(customer.name) : customer.name != null) return false;

return true;
}

@Override
public int hashCode() {
int result = idCustomer;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
}

模型.Person.java

@Entity
@Table(name = "Person")
public class Person {
private int idPerson;
private String country;

@Id
@Column(name = "idPerson")
public int getIdPerson() {
return idPerson;
}

public void setIdPerson(int idPerson) {
this.idPerson = idPerson;
}

@Basic
@Column(name = "country")
public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Person person = (Person) o;

if (idPerson != person.idPerson) return false;
if (country != null ? !country.equals(person.country) : person.country != null) return false;

return true;
}

@Override
public int hashCode() {
int result = idPerson;
result = 31 * result + (country != null ? country.hashCode() : 0);
return result;
}
}

这是我的工具dao.CustomerDAOImpl.java

public List<Customer> listCustomers() {
Session session = this.sessionFactory.getCurrentSession();
List<Customer> customersList = session.createQuery("from Customer c LEFT JOIN Person p ON p.idPerson = c.idCustomer").list();

for (Customer c : customersList) {
logger.info("Customer List::" + c);
}

return customersList;
}

我是新的 java 开发人员,英语很差有人请帮助我谢谢

最佳答案

Hibernate/JPA 构建某种数据库抽象层。因此,它与实体(而不是表)和关系(@ManyToOne ...)一起工作到实体,而不是显式连接 ID 来获取表列。

@Entity
Person {
@Id
Long id;
...
@OneToOne
@JoinColumn(name = "customer_fk")
Customer customer;
//NO other customer_fk mapping!!!
}


@Entity
Customer {
@Id
Long id;
}

现在你可以SELECT c from Person p LEFT JOIN p.customer c

关于java - 如何使用Spring+Hibernate查询join表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31402468/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com