【问题标题】:Spring JPA how to create tables so I can share common ID?Spring JPA 如何创建表以便我可以共享公共 ID?
【发布时间】:2016-11-25 20:38:39
【问题描述】:

我想在 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)我下一个表格,回复我想在“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>

【问题讨论】:

标签: java spring hibernate spring-data-jpa


【解决方案1】:

根据你的问题,我知道你想建立关系。所以你可以使用类似的东西

对话

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

// getter setter 以此类推

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

}

}

你也可以使用 ConversationMessage 和 ConversationModel 中的@ManyToOne

【讨论】:

  • 好吧,我想在“对话”中只有两个字段 = conversation_id、conversation_subject。
  • 好的,所以删除上面的列表并在其他中添加 manytoone 和 joincolumn
猜你喜欢
  • 2011-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 2012-11-08
  • 2019-11-21
  • 1970-01-01
  • 2020-01-06
相关资源
最近更新 更多