Wednesday, December 10, 2014

unknown column 'date' in 'field list'

When working on a project in which we use hibernate, I get
Unknown column 'date' in 'field list'
as exception. The spring exception was:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select this_.ID as ID3010_0_, this_.date as date3010_0_,
The only field I have in my entity named 'date' is not marked with column annotation. So it should not be mapped to database table and thus should not be used in the query right? No, think again. Every field in the entity class is mapped and column annotation is optional. So if you ever want hibernate to ignore a field in your entity (because either this field is not present in the table or you don't want to use it somehow), use transient annotation. So,
@Entity
public class MyEntity implements Serializable
{
    private String date;

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;
must become this
@Entity
public class MyEntity implements Serializable
{
    @Transient
    private String date;

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;