I am building a chat application with Spring, Hibernate and JPA and I want two tables - chats and messages. Chat will have many messages, also id and user_first and user_second. Right now I am doing it with a composite primary key where the composite is user_first and user_second and the id is unique auto-increment:
public class ChatPK implements Serializable {
protected int firstUser;
protected int secondUser;
...
@Entity
@IdClass(ChatPK.class)
@Table(name = "chats1")
public class Chat implements Serializable {
@Id
private int firstUser;
@Id
private int secondUser;
@Column(name = "id", unique = true, nullable = false, insertable = false, updatable = false)
@GeneratedValue(strategy= GenerationType.IDENTITY)
private int id;
...
is there any better way to do it and why?
Update: Chat is a uniqe row between two people there cant be two chats with the same two users. I want to take all messages that I need along with the two users that had those messages, so I can put the sender id into a hashmap key and the messages into a value. To an answer bellow SessionId is a different thing than what my table chat is doing not a different way to implement what I want.