Search code examples
hibernatepojo

Pojo class in hibernate


Hi I am new to Hibernate . I have a doubt. I have a table User. Columns are Name ,Age and Location. So my pojo class would be like this :

@Entity
@Table(name = "User")
public class User {

    @Id
    private int Age;
    private String Location;
    private String Name;
//Getters and setters methods

Now can I have another static declaration (salary is not a field in the table)

private int salary;

Will hibernate assume that even salary as a column in the table User? What exactly is the function of a POJO class in hibernate?


Solution

  • Yes, all filds are persistent by default. If you want to have a field that is not persistet, you need to annotate it with @Transient:

    @Transient
    private int salary;
    

    Of course, each time Hibernate will load a User from the database, the salary will have a default value (0, or the one set in the no-arg constructor of User).

    Please respect the Java naming conventions: fields start with a lower-case letter. Also, using the age of a user as its ID doesn't mak much sense: several users will probably have the same age, and the age is a mutable value.

    What exactly is the function of a POJO class in hibernate?

    Well, that's the definition of Hibernate: it's an ORM, which maps database tables to Java objects. Hibernate has a good documentation. Read it.