1

Im using Springboot version '2.5.1', doing some simple application by using Hibernate to query data from local database. While using Hibernate .findAll(), I've received a error message of The column name last_update was not found in this ResultSet.

I notice the issue with Hibernate for reading my lastUpdate column as last_update. Despite I have specified the field using @Column annotation.

enter image description here

Can one help to correct my mistake please?

Database

enter image description here

Entity

package com.user.learning.springboot.springboot.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.Timestamp;
import java.util.Date;

@Entity
@Table(name="student")
public class Student {
    // define fields
    @Id
    @Column (name="email")
    private String email;
    private String name;
    private Date dob;
    private Integer age;
    @Column (name="lastUpdate")
    private Timestamp lastUpdate;

    // define constructors
    public Student(){

    }

    public Student(String name, Date dob, Integer age, Timestamp lastUpdate) {
        this.name = name;
        this.dob = dob;
        this.age = age;
        this.lastUpdate = lastUpdate;
    }

    // define getters/setters - using auto generate function provided by IDE (just right click, generate)

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

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

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Timestamp getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Timestamp lastUpdate) {
        this.lastUpdate = lastUpdate;
    }


    //     define toString() method
    @Override
    public String toString() {
        return "Student{" +
                "email='" + email + '\'' +
                ", name='" + name + '\'' +
                ", dob=" + dob +
                ", age=" + age +
                ", lastUpdate=" + lastUpdate +
                '}';
    }
}

Service Implemenatation

package com.user.learning.springboot.springboot.service;

import com.user.learning.springboot.springboot.entity.Student;
import com.user.learning.springboot.springboot.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService{

    @Autowired
    StudentRepository studentRepository;

    @Override
    public List<Student> getAll() {
        return studentRepository.findAll();
    }
}

Solution tried:

  1. https://stackoverflow.com/a/38875123/4311268
Tommy Leong
  • 2,509
  • 6
  • 30
  • 54

2 Answers2

1

Although I agree it should respect your override, that really comes down to the naming strategy in use. You'll want to choose the one that works for your case. And, I'll not lie: I've always been confused by which does what

Christian Bongiorno
  • 5,150
  • 3
  • 38
  • 76
1

Finally resolved, with 2 steps.

  1. Append the following in your application.properties file (credit)
### For Hibernate 5.0
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

### How to know your Hibernate version? Simply print this line below
System.out.println(org.hibernate.Version.getVersionString());
  1. Append @Column annotation inside your @Entity (credit)
    @Column(name = "\"ageGroup\"")
    private Integer ageGroup;
    @Column(name = "\"lastUpdate\"")
    private Timestamp lastUpdate;
Tommy Leong
  • 2,509
  • 6
  • 30
  • 54