如何获取字符串值并将其保存在数据库中

时间:2012-09-20 15:43:12

标签: spring hibernate

我正在使用spring form标签和休眠..

我的jsp页面包含以下代码:

<tr><td><form:input path="userEnteredHostNameString" size="30" maxlength="200"/></td>
<td><form:input path="userEnteredDirectoryString" size="30" maxlength="200"/></td>
<td><form:input path="userEnteredUserNameString" size="20" maxlength="20"/></td>
<td><form:input path="userEnteredPasswordString" size="20" maxlength="20"/></td></tr>

我的location.java中的getter和setter

public String getUserEnteredHostNameString() {
        return userEnteredHostNameString;
    }
    public void setUserEnteredHostNameString(String userEnteredHostNameString) {
        if (userEnteredHostNameString!=null) userEnteredHostNameString = userEnteredHostNameString.toUpperCase();
        this.userEnteredHostNameString = userEnteredHostNameString;
    }
    public String getUserEnteredDirectoryString() {
        return userEnteredDirectoryString;
    }
    public void setUserEnteredDirectoryString(String userEnteredDirectoryString) {
        if (userEnteredDirectoryString!=null) userEnteredDirectoryString = userEnteredDirectoryString.toUpperCase();
        this.userEnteredDirectoryString = userEnteredDirectoryString;
    }
    public String getUserEnteredUserNameString() {
        return userEnteredUserNameString;
    }
    public void setUserEnteredUserNameString(String userEnteredUserNameString) {
        if (userEnteredUserNameString!=null) userEnteredUserNameString = userEnteredUserNameString.toUpperCase();
        this.userEnteredUserNameString = userEnteredUserNameString;
    }
    public String getUserEnteredPasswordString() {
        return userEnteredPasswordString;
    }
    public void setUserEnteredPasswordString(String userEnteredPasswordString) {
        if (userEnteredPasswordString!=null) userEnteredPasswordString = userEnteredPasswordString.toUpperCase();
        this.userEnteredPasswordString = userEnteredPasswordString;
    }

在我的控制器中,我有以下代码:

FtpScanEvents f = new FtpScanEvents();
        if(location.getUserEnteredHostNameString()!=null){
            f.setHostName(location.getUserEnteredHostNameString());
            }
        if(location.getUserEnteredDirectoryString()!=null){
            f.setDirectory(location.getUserEnteredDirectoryString());
            }
        if(location.getUserEnteredUserNameString()!=null){
            f.setUserName(location.getUserEnteredUserNameString());
            }
        if(location.getUserEnteredPasswordString()!=null){
            f.setPassword(location.getUserEnteredPasswordString());
            }
        ftpDao.save(f);

FtpScanEvents表的hostName, directory, username, password, locationId, idx, Id为列..

ftpscanevents的hbm映射文件:

<hibernate-mapping package="ca.ups.tundra.model">
    <class name="FtpScanEvents" table="FTP_SCAN_EVENTS">
        <id name="id" type="long">
            <generator class="native"/>
        </id>
        <property name="hostName" type="string" column="HOSTNAME" length="200"/>
        <property name="directory" type="string" column="DIRECTORY" length="200"/>
        <property name="userName" type="string" column="USERNAME" length="20"/>
        <property name="password" type="string" column="PASSWORD" length="20"/>
        <many-to-one name="location" class="Location" cascade="all">
            <column name="ID" not-null="true" />
        </many-to-one>
    </class>
</hibernate-mapping>

保存(e实体)代码..

@Override
    @Transactional(propagation = Propagation.MANDATORY)
    public void save(E entity) {
        if(entity == null)
            throw new NullArgumentException("entity");
        Session s = sessionFactory.getCurrentSession();
        s.saveOrUpdate(entity);
    }

ftpDao.save(f);程序终止并且控制台中没有错误时,非常感谢任何建议。

1 个答案:

答案 0 :(得分:0)

在save方法中,我错过了一个属性。添加了具有if条件的属性并且它有效!