0

我想完成以下任务:

用户可以发布带有标题和描述的消息。他也可以留下他的电子邮件地址、电话号码或两者。

我想将有关他的消息的信息存储在一个表中,其中包含 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;
    }
}

现在,这是我的联系人类: 编辑:我确实messageId在联系人类中添加了属性。我还在 Contact 表中添加了该列,并添加了一个指向 Messages 表的 id 列的外键。 @Entity @Table(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 放在了path电子邮件和电话号码字段的属性中:

<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>
4

1 回答 1

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

这意味着:

  • 对于每条消息,只有一个联系人(并且只有一个消息使用一个联系人)
  • 每当您对消息执行保存、更新或删除操作时,该操作都会级联到联系人表。
于 2013-02-05T06:07:46.710 回答