Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpringMVC和Hibernate-如何在持久化对象时将数据保存在两个表中?_Spring_Hibernate_Spring Mvc - Fatal编程技术网

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

SpringMVC和Hibernate-如何在持久化对象时将数据保存在两个表中?,spring,hibernate,spring-mvc,Spring,Hibernate,Spring Mvc,我想做到以下几点: 用户可以发布带有标题和说明的消息。他也可以留下他的电子邮件地址,电话号码,或者两者都留下 我想将关于他的消息的信息存储在一个表中,包括id、userId、title和description列。 我想将他的联系信息存储在另一个表中,包括id、messageId、phoneNumber和email列。 我希望能够列出这些消息。对于每条消息,我还想打印电话号码和电子邮件地址 由于不同的原因,我想将联系人信息存储在不同的表中,我无法将其与消息一起存储在消息表中 我不知道实现这一点的最

我想做到以下几点:

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

我想将关于他的消息的信息存储在一个表中,包括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;
    }
}
现在,这里是我的联系人类: 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>

头衔*
描述*
电话号码*
电子邮件*
这意味着:

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

您好,哪个部分不起作用?无法存储在数据库中或无法显示在页面中。看起来您的实体很好。它应该是一对一关系。当尝试加载我的消息列表时,我遇到以下异常:org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为org.hibernate.OObjectNotFoundException:不存在具有给定标识符的行:[com.domain.Contact#]好的,您的意思是数据可以存储在DB的两个表中?根据错误信息,您用于获取联系人的id似乎无法获得结果。您可以使用此id检查id是什么,以及联系人表中是否存在数据。关于Message类中的@JoinColumn注释,它不应该引用id列的名称吗在联系人表中?现在它被设置为:@JoinColumn(name=“messageId”,nullable=false),因此,它在我的表“Messages”中创建了一个名为“messageId”的列,但在“Messages”表中已经有一个名为“id”的列.wow,这肯定是个问题。@JoinColumn表示要使用哪个列连接另一个表,在这种情况下,它应该是id列。是的,这正是我想要的。现在,在尝试插入消息时,我收到以下错误消息:bean类[com.domain.Ad]的属性“phone”无效:Bean属性“phone”不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配?
@OneToOne(cascade=CascadeType.ALL)
private Contact contact;