Using Hibernate @Column annotation without getters and setters methods declaration

937 views Asked by At

This is my first contact with Hibernate and JPA annotations. Recently I got engaged in a project using Hibernate ORM and MVC and I'm facing a problem when trying run in Eclipse IDE. Suppose that I have a class declaration like this:

User.java:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Data;

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

    @Id
    @GeneratedValue
    @Column(name="id_user") 
    private int id_user;

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

    @Column(name="email") 
    private String email;

    @Column(name="accesslevel")
    private int accesslevel;

    @Column(name="username")
    private String username;
}

And if I have another class creating objects from this class, for example, in the User controller from my MVC pattern:

User admin = new User();
admin.setUsername("Thommas");

Then i get an error

"setUsername(String) is undefined for the type User"

I know that the getters and setters methods are not explicitly declared in my class. But this code was written for another person this way.

My question is:

This type of declaration using @Entity annotations is possible use the get's and set't without declaring them explicitly so what the person did it's correct and works fine, therefore the problem isn't the code but another like import correctly the libraries or Eclipse configurations

OR

This is not allowed and I must declare the getters and setters methods.

I just want to know If this is correct and the problem is in my configurations, maybe the tool isn't recognizing the annotations correctly. Thank you!

1

There are 1 answers

1
Mike Adamenko On BEST ANSWER

This project uses Lombok. You should add it as dependency and configure IDE to recognize it.