SpringMVC和Hibernate-如何在持久化对象时将数据保存在两个表中?
我想做到以下几点: 用户可以发布带有标题和说明的消息。他也可以留下他的电子邮件地址,电话号码,或者两者都留下 我想将关于他的消息的信息存储在一个表中,包括id、userId、title和description列。 我想将他的联系信息存储在另一个表中,包括id、messageId、phoneNumber和email列。 我希望能够列出这些消息。对于每条消息,我还想打印电话号码和电子邮件地址 由于不同的原因,我想将联系人信息存储在不同的表中,我无法将其与消息一起存储在消息表中 我不知道实现这一点的最佳方式是什么,尤其是知道我希望能够检索到带有消息ID的联系人信息 我用@OneToOne、@JoinColumn或@SecondaryTable尝试了多种方法,但都无法实现 以下是我现在拥有的: 消息类:SpringMVC和Hibernate-如何在持久化对象时将数据保存在两个表中?,spring,hibernate,spring-mvc,Spring,Hibernate,Spring Mvc,我想做到以下几点: 用户可以发布带有标题和说明的消息。他也可以留下他的电子邮件地址,电话号码,或者两者都留下 我想将关于他的消息的信息存储在一个表中,包括id、userId、title和description列。 我想将他的联系信息存储在另一个表中,包括id、messageId、phoneNumber和email列。 我希望能够列出这些消息。对于每条消息,我还想打印电话号码和电子邮件地址 由于不同的原因,我想将联系人信息存储在不同的表中,我无法将其与消息一起存储在消息表中 我不知道实现这一点的最
@Entity
@Table(name="messages")
public class Message implements Serializable {
private int idMessage;
private int userId;
private String title;
private String description;
private Contact contact;
@Id
@GeneratedValue
@Column(name="idMessage")
public int getId() {
return idMessage;
}
public void setId(int id) {
this.id = id;
}
@Column(name="userId", nullable=false)
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@Column(name="title", nullable=false)
public int getTitle() {
return title;
}
public void setTitle(int title) {
this.title = title;
}
@Column(name="description", nullable=false)
public int getDescription() {
return userId;
}
public void setDescription(int description) {
this.description = description;
}
@OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
@JoinColumn(name="contactId", nullable=false)
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
}
现在,这里是我的联系人类:
Edit:我确实在Contact类中添加了属性messageId
。我还在Contact表中添加了该列,并添加了一个外键,指向Messages表的id列。
@实体
@表(name=“contacts”)
公共类联系人{
private String id;
private int messageId;
private Message message;
private String phoneNumber;
private String email;
public Contact(Message message, String phoneNumber, String email) {
this.message= message;
this.phoneNumber = phoneNumber;
this.email = email;
}
@Id
@GeneratedValue
@Column(name="ID")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@OneToOne(fetch = FetchType.LAZY, mappedBy = "contact")
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
@Column(name="phoneNumber", nullable=true)
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Column(name="email", nullable=true)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
我的一些JSP代码,请注意,我确实将contact.email和contact.phoneNumber放在电子邮件和电话号码字段的路径
属性中:
<form:form method="post" commandName="ad">
<table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0"
cellpadding="5">
<tr>
<td align="right" width="20%">Title *</td>
<td width="20%"><form:input size="64" path="title" class="input-xxlarge"/></td>
<td width="60%"><form:errors path="title" cssClass="error" /></td>
</tr>
<tr>
<td align="right" width="20%">Description *</td>
<td width="20%">
<%-- <form:input path="description" /> --%> <textarea rows="3"></textarea>
</td>
<td width="60%"><form:errors path="description"
cssClass="error" /></td>
</tr>
<tr>
<td align="right" width="20%">Phone Number *</td>
<td width="20%"><form:input path="contact.phoneNumber" /></td>
<td width="60%"><form:errors path="contact.phoneNumber" cssClass="error" /></td>
</tr>
<tr>
<td align="right" width="20%">Email *</td>
<td width="20%"><form:input path="contact.email" /></td>
<td width="60%"><form:errors path="contact.email" cssClass="error" /></td>
</tr>
</table>
头衔*
描述*
电话号码*
电子邮件*
这意味着:
- 对于每条消息,只有一个联系人(一个联系人仅用于一条消息)
- 无论何时对邮件执行保存、更新或删除操作,该操作都会级联到联系人表
@OneToOne(cascade=CascadeType.ALL)
private Contact contact;