【问题标题】:Spring MVC and Hibernate - How to save data in two tables when persisting an object?Spring MVC 和 Hibernate - 持久化对象时如何将数据保存在两个表中?
【发布时间】:2013-02-06 12:32:20
【问题描述】:

我想完成以下工作:

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

我想将有关他的消息的信息存储在一个表中,其中包含 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(name="联系人") 公共类联系人{

    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>

【问题讨论】:

  • 您好,哪个部分不起作用?无法存储在数据库中或无法在页面中显示。看来你的实体很好。应该是一对一的关系。
  • 在尝试加载我的消息列表时,出现以下异常:org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行:[com.domain.Contact#]
  • 好的,你的意思是数据可以存储在数据库的两个表中?根据错误信息,您用于获取联系人的 id 似乎无法获取结果。您可以使用此 id 检查 id 是什么以及数据是否存在于 Contact 表中。
  • 关于Message类中的@JoinColumn注解,不应该引用Contact表中id列的名称吗?现在它设置为以下内容:@JoinColumn(name="messageId", nullable=false),因此,它在我的表“消息”中创建了一个名为“messageId”的列,但我已经在名为“id”的“消息”表。
  • 哇,这绝对是个问题。 @JoinColumn 表示你想用哪一列连接另一个表,这种情况下应该是 id 列。

标签: spring hibernate spring-mvc


【解决方案1】:
@OneToOne(cascade=CascadeType.ALL)
private Contact contact;

这意味着:

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

【讨论】:

  • 是的,这正是我想要的。
  • 现在,在尝试插入消息时,我收到以下错误消息: bean 类 [com.domain.Ad] 的无效属性“电话”:Bean 属性“电话”不可读或有一个无效的getter方法:getter的返回类型是否与setter的参数类型匹配?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-28
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多