如何使用hibernate将textarea输入存储到数据库中?

时间:2012-08-13 18:49:07

标签: java database hibernate textarea

我在一个名为information.jsp的jsp文件中创建了一个textarea,我试图让它在用户点击提交时将输入存储到数据库中。我已经制作了information.hbm.xml文件,如下所示:

information.jsp:

<textarea id = "desc" rows="5" cols="115" onkeypress="textCounter(this,20);"><c:out value="${informationView.storedDescription}"/></textarea>

information.hbm.xml

<hibernate-mapping>
  <class name="Information" table="INFO_USER">
      <id name="id" type="long" column="INFO_ID">
  <generator class="native">
    <param name="sequence">ID_SEQ</param>
  </generator>
</id>

  <property name="description" column="DESC"/>

</class>
</hibernate-mapping>

然后,我创建了一个类Information,其中包含用于描述的getter和setter,用于存储和检索数据库中的信息。我只是想弄清楚如何从提交事件中获取textarea的描述输入......

从我一直在阅读的内容来看,我认为当有人点击提交但又不确定时,我必须让InformationAction实际保存。我是Hibernate的新手,在将输入保存到数据库并在有人重新打开页面的情况下检索它以自动加载到textarea的过程中出现错误。

我无法弄清楚我是如何将输入从textarea传递到数据库的。 任何帮助都会很棒,因为我已经在这方面工作了很长时间,但无法理解。如果您需要更多信息,请告诉我,谢谢。

1 个答案:

答案 0 :(得分:0)

是的,您需要InformationActionInformationContoller,具体取决于您要使用的网络框架。您的操作或控制器需要具有映射到文本区域值的description属性。如果你使用像Struts2或Spring MVC这样的Web框架,这很容易实现。

现在进入休眠部分。您的操作需要具有可以读取和写入数据库值的hibernate Session对象。然后,您可以使用前端获得的Information构建description对象,然后在会话中调用saveOrUpdate()方法。

代码将是这样的

public class InformationAction {
    //Maps to text area value. Needs getter and setter
    private String description;

    //Inject session from hibernate configuration
    private Session session;

    public void someMethod() {
     Information information = new Information();
     information.setDescription(description);
     session.saveOrUpdate(information);
   }
}

这将在信息表中保存一行。