Вы можете попробовать это:
Класс сообщения:
@Entity
public class Message {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String text;
private String tag;
@ManyToOne
@JoinColumn(name = "user_id")
private User userId;
}
Класс пользователя:
@Entity
@Table(name = "usr")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String username;
private String password;
private boolean active;
@ElementCollection(targetClass = Role.class , fetch = FetchType.LAZY)
@CollectionTable(name = "user_role" , joinColumns = @JoinColumn(name = "user_id"))
@Enumerated(EnumType.STRING)
private Set<Role> roles;
@OneToMany(mappedBy = "user")
@JsonBackReference
private set<Message> messages;
}