【问题标题】:Get com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column获取 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column
【发布时间】:2016-03-08 21:46:27
【问题描述】:

我决定测试 Spring Boot。我的项目有下一个依赖项:JPA、MySql、WEB。我创建了简单的 MySql 数据库。这是一张表格:

CREATE TABLE `rawtype` (
  `rtId` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `rtName` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`rtId`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

这是此表的域:

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name="rawtype")
public class Rawtype implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="rtId", nullable = false)
    @GeneratedValue
    private int rtId;

    @Column(name="rtName", nullable = false)
    private String rtName;

    protected Rawtype() {
    }

    public Rawtype(int rtId, String rtName) {
        this.rtId = rtId;
        this.rtName = rtName;
    }

    public int getRtId() {
        return rtId;
    }

    public void setRtId(int rtId) {
        this.rtId = rtId;
    }

    public String getRtName() {
        return rtName;
    }

    public void setRtName(String rtName) {
        this.rtName = rtName;
    }

    @Override
    public String toString() {
        return "Rawtype{" +
                "rtId=" + rtId +
                ", rtName='" + rtName + '\'' +
                '}';
    }
}

尝试使用 JpaRepository 方法从该表中获取所有行

  List<T> findAll();

在日志中我看到 Hibernate 执行了这个查询:

select rawtype0_.rt_id as rt_id1_0_, rawtype0_.rt_name as rt_name2_0_ from rawtype rawtype0_

我得到这个错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'rawtype0_.rt_id' in 'field list'

谁能建议我该怎么做? 谢谢。

附言

RawtypeRepository.java

import org.springframework.data.jpa.repository.JpaRepository;
import domain.Rawtype;

public interface RawtypeRepository extends JpaRepository<Rawtype,Integer> {
    }

RawtypeServiceImpl.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import domain.Rawtype;

import java.util.List;

@Service
@Transactional
public class RawtypeServiceImpl implements RawtypeService{

    @Autowired
    RawtypeRepository rawtypeRepository;

    public List<Rawtype> findAll() {
        return rawtypeRepository.findAll();
    }
}

【问题讨论】:

  • 所以,列“rawtype0_.rt_id”不存在
  • 是的,它不存在。我有列 rtId,但没有 rt_id。我不明白 Hibernate 是如何生成这个 rt_id 的。
  • 嗯,能发一下创建hibernate查询的方法吗?
  • Hibernate 似乎以某种方式忽略了您的 JPA 注释。我认为this 的答案可以解决您的问题。
  • @zigfridus,这就是我的意思。自己回答您自己的问题,然后将其标记为已接受。是你找到了解决方案,而不是我。我只是指出了它的路径。

标签: java mysql spring hibernate jpa


【解决方案1】:

感谢@aribeiro 为我提供了这个link 我发现了我的错误。 列的名称不应为驼峰式。所以我改了

@Column(name="rtId", nullable = false) 到

@Column(name="rtid", nullable = false)

现在程序可以正常工作了。

【讨论】:

    猜你喜欢
    • 2015-01-22
    • 2016-09-18
    • 1970-01-01
    • 2017-09-10
    • 2012-04-20
    • 2013-11-20
    • 2016-07-07
    • 2018-04-26
    • 2012-07-20
    相关资源
    最近更新 更多