我对 Hibernate 和 MySQL 数据库还很陌生。我在从数据库中检索系统版本时遇到问题。当尝试“select s from System s left join fetch s.releases”时,我收到以下错误:org.hibernate.exception.SQLGrammarException: could not extract ResultSet。
在服务器错误的根本原因部分它说:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'release release1_ on system0_.id=releases1_.system_id' 附近使用正确的语法
我试图弄清楚用于检索系统列表及其相应版本的 HQL 查询是什么。我在这里看了很多例子,但结果是一样的。
我的实体如下:
系统.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
发布.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;