Здравствуйте.
Для начала Вам необходимо иметь аннотированный класс, связанный с сущностью в базе данных. Для примера приведу из своего проекта:
@Entity
@Table(name = "users")
public class User implements Serializable {
@Id
@Column(name = "user_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator = "users_user_id_seq")
@SequenceGenerator(name = "users_user_id_seq",
sequenceName = "users_user_id_seq",
allocationSize = 1)
private long userId;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "login", nullable = false, unique = true)
private String login;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "email", nullable = false, unique = true)
private String email;
@Column(name = "enabled", nullable = false)
private boolean isEnabled;
@Column(name = "reg_date", nullable = false)
private Date regDate;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "authority_id", nullable = false)
private Authority authority;
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
@JsonIgnore
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean isEnabled() {
return isEnabled;
}
public void setEnabled(boolean enabled) {
isEnabled = enabled;
}
@JsonSerialize(using = SqlDateSerializer.class)
public Date getRegDate() {
return regDate;
}
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
public Authority getAuthority() {
return authority;
}
public void setAuthority(Authority authority) {
this.authority = authority;
}
@Override
public String toString() {
return "User details" +
"Id = " + getUserId() + "\n" +
"Name = " + getName() + "\n" +
"Login = " + getLogin() + "\n" +
"Email = " + getEmail() + "\n" +
"Enabled = " + isEnabled() + "\n" +
"Role = " + getAuthority().getAuthority().name() + "\n" +
"Registration date = " + getRegDate();
}
Если Вам будут непонятны какие-либо вещи из класса сущности, напишите в комментарии, я отвечу.
Далее при конфигурировании Hibernate вам нужно указывать не
.addClass(MyClass.class)
, а
configuration.addAnnotatedClass(Main.class)
.