2016-11-25 86 views
-1

我想在Spring中創建消息系統。我將有3個模型和2個表單。我想要做的是:
1)創建一個表「會話」=完成
2)創建新的條目與對話ID和對話主題=完成表「談話」。
4)創建新表 「conversations_members」
5)在表創建兩個新進 「conversations_members」,一個與conversation_idsender_idlastviewed dateisdeleted = 0 (false)conversation_idreceiver_idlastviewed_dateisdeleted=0(false)
6)餘下的形式,與回覆我想用message_id(autogenerated,Long id),conversation_id,message_date和messate_text在「conversationations_messages」中生成條目。

我不知道如何連接3個表中的所有conversation_id。我認爲這將是「一對多」的選擇,但如何使用它? 最好的問候,Jędrzej。Spring JPA如何創建表以便我可以共享公共ID?

對話模型:

@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> 
+0

是的,你需要使用關係。只需使用註釋即可。請參閱http://www.javatpoint.com/spring-and-jpa-integration – user1211

回答

0

根據你的問題,你想使relation.so你可以使用類似的東西

對話

@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; 

@OnetoMany(fetch=FetchType.LAZY,cascade=CascadeType.MERGE) 
private List<ConverstaionMessage> message; 

@OnetoMany(fetch=FetchType.LAZY,cascade=CascadeType.MERGE) 
private List<ConverstaionModel> message; 

//吸氣setter方法讓我明白on

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() { 

} 

} 

也可以使用 @ManyToOne在ConversationMessage和ConversationModel中

+0

以及我想在「對話」中只有兩個字段= conversation_id,conversation_subject。 – DEADALICE7000

+0

確定刪除上面的列表,並在其他人中添加manytoone和joincolumn –

相關問題