gpt4 book ai didi

java - 获取 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Unknown column

转载 作者:行者123 更新时间:2023-11-29 21:16:42 25 4
gpt4 key购买 nike

我决定测试 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();
}
}

最佳答案

感谢@aribeiro 为我提供了这个 link我发现了我的错误。列的名称不应采用驼峰命名法。因此我改变了

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

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

现在程序运行良好。

关于java - 获取 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Unknown column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35878650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com