【发布时间】:2016-11-25 20:38:39
【问题描述】:
我想在 Spring 中创建消息系统。我将有 3 个模型和 2 个表格。我想做的是:
1)创建一个表“对话”=完成
2)在表“对话”中创建新条目,其中包含对话 ID 和对话主题 = 完成。
4)创建新表“conversations_members”
5)在表“conversations_members”中创建两个新条目,一个为conversation_id、sender_id、lastviewed date、isdeleted = 0 (false) 和conversation_id、receiver_id、lastviewed_date、isdeleted=0(false)
6)我下一个表格,回复我想在“conversations_messages”中生成带有 message_id(自动生成,长 id)、conversation_id、message_date 和 message_text 的条目。
我不知道如何在 3 个表中连接所有 conversation_id。我认为这将是“一对多”选项,但如何使用它呢?
最好的问候,Jędrzej。
对话模型:
@Entity
@Table(name = "conversation")
public class Conversation {
@Id
@Column(name = "conversation_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "conversation_subject")
private String subject;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public Conversation(String subject) {
this.subject = subject;
}
public Conversation() {
}
}
ConversationMembers 模型:
@Entity
@Table(name="conversation_members")
public class ConversationMembers {
@Column(name="conversation_id")
private Conversation conversation;
@Column(name="user_id")
private SiteUser user;
@Column(name="conversation_deleted", columnDefinition="Integer(0,1) default '0'")
private int conversationDeleted;
@Column(name="column_last_viewed")
private Date date;
public Conversation getConversation() {
return conversation;
}
public void setConversation(Conversation conversation) {
this.conversation = conversation;
}
public SiteUser getUser() {
return user;
}
public void setUser(SiteUser user) {
this.user = user;
}
public int getConversationDeleted() {
return conversationDeleted;
}
public void setConversationDeleted(int conversationDeleted) {
this.conversationDeleted = conversationDeleted;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public ConversationMembers(Conversation conversation, SiteUser user, int conversationDeleted, Date date) {
this.conversation = conversation;
this.user = user;
this.conversationDeleted = conversationDeleted;
this.date = date;
}
}
ConversationMessage 模型:
@Entity
@Table(name="conversations_messages")
public class ConversationMessages {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="message_id")
private Long messsageId;
@Column(name="conversation_id")
private Conversation conversation;
@Column(name="message_date")
private Date message_date;
@Column(name="message_text")
private String text;
public Long getMesssageId() {
return messsageId;
}
public void setMesssageId(Long messsageId) {
this.messsageId = messsageId;
}
public Conversation getConversation() {
return conversation;
}
public void setConversation(Conversation conversation) {
this.conversation = conversation;
}
public Date getMessage_date() {
return message_date;
}
public void setMessage_date(Date message_date) {
this.message_date = message_date;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public ConversationMessages(Long messsageId, Conversation conversation, Date message_date, String text) {
this.messsageId = messsageId;
this.conversation = conversation;
this.message_date = message_date;
this.text = text;
}
}
NewConversation.jsp
<form:form commandName="conversation">
<br>
<form:form commandName="conversationmembers">
To:
<form:input path="userid" type="text" name="userid" />
SUBJECT
<form:input path="subject" type="text" name="subject" />
<button type="submit" value="start conversation" />
</form:form>
</form:form>
【问题讨论】:
-
是的,你需要使用关系。只需使用注释即可。参考javatpoint.com/spring-and-jpa-integration
标签: java spring hibernate spring-data-jpa