Search code examples
hibernatejpahibernate-mapping

How does JPA map a column name to a field if the column name is different than the field name?


Suppose I have a table Students which has columns varchar firstname and varchar lastname.

For some reason my Entity Class has fields with different names, say String fname and String lname.

So how does JPA correctly map the column with the field?


Solution

  • You may use the @Column annotation, e.g.

    @Entity(name="Students")
    public class Students {
        @Column(name = "firstname")
        private String fname;
    
        @Column(name = "lastname")
        private String lname;
    }