0

I have a mini messaging app. so i a made a MessagingEnity class to store messages between 2 Users, it looks like this

 @Entity
@Table(name = "Message")
public class Message extends DefaultEntity {


    @Valid
    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name="sender")
    private User sender;

    @Valid
    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name="receiver")
    private User receiver;


    @NotNull
    private String message;

    @Enumerated(EnumType.STRING)
    private MessageStatus messageStatus;

    private Boolean isRead;
}

Then i have a UserEnity that looks like this:

@Component
@Entity
@Table(name = "Users")
public class User extends DefaultEntity {


    @Column(name = "FirstName")
    @NotNull(message = "Enter a FirstName")
    private String firstName;


    @Column(name = "LastName")
    @NotBlank(message = "Enter a LastName")
    private String lastName;


    @Column(unique = true,name = "UserName")
    @NotBlank(message = "Enter a UserName")
    private String userName;

     }

I feel i did not map it well, because i am having issues with the code in my app and especially since i know i did not properly map it in onetoone, oneto many or many to many as i confused how to do about it. But a Message has two users, who can constantly send a message between themselves, so a UserEnitity should have a list of MessageEntity.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Thanus
  • 411
  • 2
  • 8
  • 21
  • I would recommend to read this book: https://www.apress.com/de/book/9781484234198 IMHO it's the best book about JPA2 – Simon Martinelli Jun 17 '18 at 08:20
  • I found the answer in this post: https://stackoverflow.com/questions/37000890/how-to-use-mappedby-with-more-than-one-value-jpa – Thanus Jun 17 '18 at 15:11

0 Answers0