hql一对多/多对一选择

时间:2013-11-14 03:12:23

标签: java mysql hibernate

我是Hibernate和MySQL数据库的新手。我在从数据库中检索系统版本时遇到问题。当尝试 “从系统中选择s左连接提取s.releases” 时出现以下错误: org.hibernate.exception.SQLGrammarException:无法提取ResultSet

在服务器错误的根本原因部分中,它说:您的SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第1行的“system0_.id = releases1_.system_id”上的“release releases1_”附近使用正确的语法

我正在试图找出用于检索系统列表的HQL查询及其相应的版本。我在这里看了很多例子,但结果是一样的。

我的实体如下:

System.java

package com.wayne.edu.entities;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "system")
public class System {

    @Id @GeneratedValue private long id;
    private String name;
    private String issueTrackerUrl;
    private String programmingLang;
    private String versionControlUrl;

    @OneToMany(mappedBy = "system", fetch = FetchType.LAZY)
    private List<Release> releases;
    /**********************************************************************/
    Getters and Setters

Releases.java

package com.wayne.edu.entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "release")
public class Release {

    @Id
    @GeneratedValue
    private long id;

    private String name;

    @ManyToOne
    @JoinColumn(name = "system_id", referencedColumnName = "id")
    private System system;

    /**********************************************************************/
    Getters and Setters

SystemDAO.java

package com.wayne.edu.entities;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@SuppressWarnings({"unchecked", "rawtypes"})
public class SystemDAO {

    @Autowired private SessionFactory sessionFactory;

    /**
     * @Transactional annotation below will trigger Spring Hibernate transaction manager to automatically create
     * a hibernate session. See src/main/webapp/WEB-INF/servlet-context.xml
     */
    @Transactional
    public List<System> findAll() {
        Session session = sessionFactory.getCurrentSession();
        List systems = session.createQuery("from System").list();
        return systems;
    }

    @Transactional
    public List<System> findAllWithReleases() {
        Session session = sessionFactory.getCurrentSession();
        List systems = session.createQuery("select s from System s left join fetch s.releases").list();
        return systems;
    }

}

控制器

package com.wayne.edu;


import com.wayne.edu.entities.*;
import com.wayne.edu.entities.System;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/edu")
public class HomeController {

    @Autowired
    public SystemDAO systemDAO;

    @RequestMapping(value = "/hibernate", method = RequestMethod.GET)
    public String list(Model model) {
        List<System> systems = systemDAO.findAll();
        model.addAttribute("systems", systems);

        List<System> systemsWithReleases = systemDAO.findAllWithReleases();
        model.addAttribute("systemsWithReleases", systemsWithReleases);
        return "hibernate";
    }
  .
  .
  .

MYSQL表:

CREATE TABLE `release` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `system_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `system_id_idx` (`system_id`),
  CONSTRAINT `system_id` FOREIGN KEY (`system_id`) REFERENCES `system` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

CREATE TABLE `system` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `issueTrackerUrl` varchar(45) DEFAULT NULL,
  `programmingLang` varchar(45) DEFAULT NULL,
  `versionControlUrl` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

2 个答案:

答案 0 :(得分:1)

使用获取系统     从系统中选择 并使用system.getReleases()方法获取版本。

答案 1 :(得分:0)

我能够解决这个问题,显然“发布”这个词在MySQL中有一个我没有意识到的预定义,所以当我试图检索特定系统的版本时,它导致我的查询出错。 / p>