【问题标题】:Join two tables HQL query连接两个表 HQL 查询
【发布时间】:2014-12-14 20:44:01
【问题描述】:

如何使用 HQL 连接两个表?

首先,这是我对两个表的 SQL 创建查询:

CREATE TABLE `subject` (
    `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(50) NOT NULL,
    PRIMARY KEY (`id`)
)

CREATE TABLE `employee` (
    `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
    `subject_id` INT(11) UNSIGNED NOT NULL,
    `surname` VARCHAR(50) NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `FK_employee_subject` (`subject_id`),
    CONSTRAINT `FK_employee_subject` FOREIGN KEY (`subject_id`) REFERENCES `subject` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)

我正在使用 Netbeans,这是我生成的实体。

主题实体:

@Entity
@Table(name = "subject", catalog = "university")
public class Subject implements java.io.Serializable {

    private Integer id;
    private String name;
    private Set<Employee> employees = new HashSet<Employee>(0);
    private Set<Report> reports = new HashSet<Report>(0);

    public Subject() {
    }

    public Subject(String name) {
        this.name = name;
    }

    public Subject(String name, Set<Employee> employees, Set<Report> reports) {
        this.name = name;
        this.employees = employees;
        this.reports = reports;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "name", nullable = false, length = 50)
    public String getName() {
        return this.name;
    }

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

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "subject")
    public Set<Employee> getEmployees() {
        return this.employees;
    }

    public void setEmployees(Set<Employee> employees) {
        this.employees = employees;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "subject")
    public Set<Report> getReports() {
        return this.reports;
    }

    public void setReports(Set<Report> reports) {
        this.reports = reports;
    }

}

员工实体:

@Entity
@Table(name = "employee", catalog = "university")
public class Employee implements java.io.Serializable {

    private Integer id;
    private Subject subject;
    private String surname;
    private Set<Report> reports = new HashSet<Report>(0);

    public Employee() {
    }

    public Employee(Subject subject, String surname) {
        this.subject = subject;
        this.surname = surname;
    }

    public Employee(Subject subject, String surname, Set<Report> reports) {
        this.subject = subject;
        this.surname = surname;
        this.reports = reports;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "subject_id", nullable = false)
    public Subject getSubject() {
        return this.subject;
    }

    public void setSubject(Subject subject) {
        this.subject = subject;
    }

    @Column(name = "surname", nullable = false, length = 50)
    public String getSurname() {
        return this.surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "employee")
    public Set<Report> getReports() {
        return this.reports;
    }

    public void setReports(Set<Report> reports) {
        this.reports = reports;
    }

}

我尝试过使用这样的查询,但它不起作用:

select employee.id, employee.surname, subject.name from Employee employee, Subject subject where employee.subject_id=subject.id

这是我的堆栈跟踪,使用建议的查询后

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: by near line 1, column 102 [select employee.id, employee.surname, subject.name from by.bsuir.yegoretsky.model.Employee employee, by.bsuir.yegoretsky.model.Subject subject where employee.subject_id=subject.id]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
    at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796)

还有截图:

【问题讨论】:

    标签: java mysql join hql


    【解决方案1】:

    HQL 使用实体名称和实体属性名称。永远不要使用表名或列名。实体Employee 中没有subject_id 属性。

    我建议您阅读有关 HQL 的文档,尤其是有关 joins and associations 的文档。

    你需要的查询是

    select employee.id, employee.surname, subject.name from Employee employee
    join employee.subject subject
    

    【讨论】:

    • 这正是我所期待的。谢谢。
    • 如何迭代
    【解决方案2】:

    希望它可以工作

    String hql = "from Subject as subject, Employee as emp";
    
    List<?> list = session.createQuery(hql).list();
    
    for(int i=0; i<list.size(); i++) {
        Object[] row = (Object[]) list.get(i);
        Subject subject= (Subject )row[0];
        Employee employee = (Employee)row[1];
    }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 2012-08-19
      • 1970-01-01
      相关资源
      最近更新 更多