SQLGrammarException。 hibernate中的未知列名

时间:2014-07-25 23:43:28

标签: java hibernate

我有一个数据库,我有国家/地区表。国家/地区表格具有以下结构。

CREATE TABLE `countries` (
  `idCountry` int(5) NOT NULL AUTO_INCREMENT,
  `countryCode` char(2) NOT NULL DEFAULT '',
  `countryName` varchar(45) NOT NULL DEFAULT '',
  PRIMARY KEY (`idCountry`)
) ENGINE=MyISAM AUTO_INCREMENT=252 DEFAULT CHARSET=utf8;

我有一个在hibernate配置文件中正确配置的休眠Pojo。 pojo看起来像这样

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "countries")
public class Country {

    @Id
    @Column(name = "id")
    public int id;

    @Column(name = "code")
    public String code;

    @Column(name = "name")
    public String name;

    public int getId() {
    return id;
    }

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

    public String getCode() {
    return code;
    }

    public void setCode(String code) {
    this.code = code;
    }

    public String getName() {
    return name;
    }

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

}

数据库已填充了所有国家/地区和相应值的列表。但是,当我尝试使用以下代码从表中获取contry时

SessionFactory sf = SessionFactoryUtil.getSessionFactory();
    Session session = sf.openSession();
    session.beginTransaction();
    Country c = (Country) session.get(Country.class, 105);
    return c;

我收到以下异常

[org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'country0_.id' in 'field list'

1 个答案:

答案 0 :(得分:1)

改变这个:

@Id
@Column(name = "id")
public int id;

@Column(name = "code")
public String code;

@Column(name = "name")
public String name;

对此:

@Id
@Column(name = "idCountry")
public int id;

@Column(name = "countryCode")
public String code;

@Column(name = "countryName")
public String name;