我使用在Tomcat上运行的Spring MVC + Hibernate + MySql和Intellij Editor
我想像这样查询像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
这是我的代码
model.Customer.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;
}
}
model.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开发人员,英文不好有人请帮帮我谢谢
答案 0 :(得分:1)
Hibernate / JPA构建某种数据库抽象层。因此,它可以与Entity(而不是表格)和关系(@ManyToOne ...)一起使用实体,而不是明确地使用Joining Ids来获取表格列。
@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