Spring MVC和Hibernate - 如何在持久化对象时将数据保存在两个表中?

时间:2013-02-05 02:19:58

标签: spring hibernate spring-mvc

我想完成以下任务:

用户可以发布带有标题和说明的消息。他也可以留下他的电子邮件地址,电话号码或两者兼而有之。

我想将有关其消息的信息存储在一个表中,其中包含列id,userId,title和description。 我想将关于他的联系信息的信息存储在另一个表中,列id,messageId,phoneNumber和email。 我希望能够列出这些消息。对于每封邮件,我还要打印电话号码和电子邮件地址。

我想将联系信息存储在不同的表中,原因各不相同,我无法将它们与消息一起存储在消息表中。

我不知道实现这一目标的最佳方式是什么,尤其是我知道我希望能够使用消息的ID检索联系信息。

我尝试了多种方式,使用@ OneToOne,@ JoinColumn或@SecondaryTable,但无法使其正常工作。

这就是我现在所拥有的:

消息类:

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

现在,这是我的联系班: 编辑:我确实在Contact类中添加了属性messageId。我还在Contact表中添加了列,并添加了一个指向Messages表的id列的外键。     @实体     @table(名称= “联系人”)     公共课程联系{

    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代码,请注意我确实在电子邮件和电话号码字段的path属性中放了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>

1 个答案:

答案 0 :(得分:1)

@OneToOne(cascade=CascadeType.ALL)
private Contact contact;

这意味着:

  • 对于每条消息,只有一个联系人(只有一个消息使用一个联系人)
  • 每当您对邮件执行保存,更新或删除操作时,该操作都会级联到联系人表。