0

I have a database where I have countries table. The countries table has following structure.

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;

I have a hibernate Pojo which is configured properly in hibernate configuration file. The pojo looks like this

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;
    }

}

The database is already populated with list of all the countries and corresponding values. However when I try to get a contry from the table using the following code

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

I am getting the following exception

[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

1 Answer 1

1

Change this:

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

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

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

To this:

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

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

@Column(name = "countryName")
public String name;
Sign up to request clarification or add additional context in comments.

4 Comments

It wasnt working when I kept the names as is. But now it does. Thanks a lot!!
No, he should use the names provided in his model class. These are much better than the current column names.
does that mean I can not use different names? other than the one specified in the sql structure?
@RJadhav you can have different names for your attributes, and use the @Column annotation to identify it otherwise Hibernate would not have a way to tell which is for what. The normal way is to have the attributes with the same names. It normally happens when you have legacy tables to map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.