Hibernate One-To-Many关系不起作用?

时间:2012-09-17 20:15:56

标签: hibernate jstl one-to-many

Folowing是我的location.hbm.xml文件

<hibernate-mapping package="ca.ups.tundra.model">
    <class name="Location" table="LOCATION">
        <id name="id" access="field" type="long">
            <generator class="native"/>
        </id>
        <version name="version" access="field" column="VERSION" type="long"/>

        <property name="slic" type="string" column="SLIC" length="5" not-null="true"/>
        <property name="pkgSlic" type="string" column="PKGSLIC" length="5" not-null="true"/>
        <property name="name" type="string" column="NAME" length="30" not-null="true"/>

        <set name="groups" table="LOCATIONGROUPS" cascade="save-update" access="field">
            <key column="LOCATION_ID"/>
            <many-to-many class="LocationGroup" column="GROUP_ID"/>
        </set>
        <set name="fEvents" table="FTP_SCAN_EVENTS" inverse="true" lazy="true" fetch="select" cascade="save-update, delete">
            <key column ="LOCATION_ID" not-null="true" />
            <one-to-many class="FtpScanEvents" />
        </set>
    </class>
</hibernate-mapping>

以下是我的ftpscanevents.hbm.xml

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

我的location.java

public class Location extends Persistent implements  Serializable {
    private String slic;
    private String pkgSlic;
    private String name;
Set<LocationGroup> groups = new HashSet<LocationGroup>();
private Set<FtpScanEvents> fEvents = new HashSet<FtpScanEvents>();
//getters and setters
}

FtpScanEvents.java

public class FtpScanEvents extends Persistent implements Serializable{

    private String hostName;
    private String directory;
    private String userName;
    private String password;
    private Location location;
//getters and setters
}

我的Jsp,我想在这两个表中显示值;

<form:form name="f" action="${flowExecutionUrl}" modelAttribute="location">
<table>
    <tr>
    <th colspan="3">Location Add/Edit</th>
    </tr>
    <tr>
    <td>Location Name</td>
    <td><form:input path="name" size="30" maxlength="30" onkeypress="return lowerUpperLetterNumberDashPeriodSpaceEnterOnly();"/></td>
    <td class="error"><form:errors path="name"/></td>
    </tr>
    <tr>
    <td>Port Code</td>
    <td><form:input path="slic" size="30" maxlength="5"  onkeypress="return toUpperLetterNumberOnly();"/></td>
        <td class="error">
        <form:errors path="slic"/>
        <c:if test="${not empty duplicateLocationMessageKey}">
        <p><fmt:message key="${duplicateLocationMessageKey}"/></p>
        </c:if> 
        </td>   </tr>   <tr>
        <td>Pkg. SLIC</td>
        <td><form:input path="pkgSlic" size="30" maxlength="5"  onkeypress="return toUpperLetterNumberOnly();"/></td>
        <td class="error">
        <form:errors path="pkgSlic"/>
        <c:if test="${not empty duplicateLocationMessageKey}">
        <p><fmt:message key="${duplicateLocationMessageKey}"/></p>
        </c:if> 
        </td>   </tr>
<tr><td colspan="4">
        <table>

            <tr><td>Host Name</td><td>Directory</td><td>User Name</td><td>Password</td></tr>
        <tr><td><form:input path="fEvents.hostName" id="hostName" size="30" maxlength="200"/></td>
            <td><form:input path="fEvents.directory" id="directory" value="" size="30" maxlength="200"/></td>
            <td><form:input path="fEvents.userName" id="userName" value="" size="20" maxlength="20"/></td>
            <td><form:input path="fEvents.password" id="password" value="" size="20" maxlength="20"/></td>
            <td><input type="button" id="delete" onclick="if(confirm( 'Do you want to delete')) deleteRow(this);" value="-" /></td></tr>
        </table>
            </td></tr>
</table>
</form:form>

当我运行我的应用程序并导航到location.jsp时。我收到了以下错误:

SEVERE: Servlet.service() for servlet jsp threw exception
ognl.NoSuchPropertyException: org.hibernate.collection.PersistentSet.hostName
    at ognl.ObjectPropertyAccessor.getProperty(ObjectPropertyAccessor.java:122)
    at ognl.SetPropertyAccessor.getProperty(SetPropertyAccessor.java:60)
    at ognl.OgnlRuntime.getProperty(OgnlRuntime.java:1657)
    at ognl.ASTProperty.getValueBody(ASTProperty.java:92)
    at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:170)
    at ognl.SimpleNode.getValue(SimpleNode.java:210)
    at ognl.ASTChain.getValueBody(ASTChain.java:109)
    at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:170)
    at ognl.SimpleNode.getValue(SimpleNode.java:210)
    at ognl.Ognl.getValue(Ognl.java:333)
    at org.springframework.binding.expression.ognl.OgnlExpression.getValue(OgnlExpression.java:85)
    at org.springframework.webflow.mvc.view.BindingModel.getFormattedValue(BindingModel.java:240)
    at org.springframework.webflow.mvc.view.BindingModel.getFieldValue(BindingModel.java:141)
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:172)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:192)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:158)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:121)
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:379)
    at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:139)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:90)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)
    at org.apache.jsp.WEB_002dINF.jsp.Location_jsp._jspx_meth_form_005finput_005f10(Location_jsp.java:2398)
    at org.apache.jsp.WEB_002dINF.jsp.Location_jsp._jspService(Location_jsp.java:653)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:236)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:257)
    at org.springframework.webflow.mvc.servlet.ServletMvcView.doRender(ServletMvcView.java:50)
    at org.springframework.webflow.mvc.view.AbstractMvcView.render(AbstractMvcView.java:180)
    at org.springframework.webflow.engine.ViewState.render(ViewState.java:282)
    at org.springframework.webflow.engine.ViewState.doEnter(ViewState.java:186)
    at org.springframework.webflow.engine.State.enter(State.java:194)
    at org.springframework.webflow.engine.Flow.start(Flow.java:535)
    at org.springframework.webflow.engine.impl.FlowExecutionImpl.start(FlowExecutionImpl.java:364)
    at org.springframework.webflow.engine.impl.RequestControlContextImpl.start(RequestControlContextImpl.java:234)
    at org.springframework.webflow.engine.SubflowState.doEnter(SubflowState.java:101)
    at org.springframework.webflow.engine.State.enter(State.java:194)
    at org.springframework.webflow.engine.Transition.execute(Transition.java:227)
    at org.springframework.webflow.engine.impl.FlowExecutionImpl.execute(FlowExecutionImpl.java:391)
    at org.springframework.webflow.engine.impl.RequestControlContextImpl.execute(RequestControlContextImpl.java:214)
    at org.springframework.webflow.engine.TransitionableState.handleEvent(TransitionableState.java:119)
    at org.springframework.webflow.engine.Flow.handleEvent(Flow.java:555)
    at org.springframework.webflow.engine.impl.FlowExecutionImpl.handleEvent(FlowExecutionImpl.java:386)
    at org.springframework.webflow.engine.impl.RequestControlContextImpl.handleEvent(RequestControlContextImpl.java:210)
    at org.springframework.webflow.engine.ViewState.handleEvent(ViewState.java:230)
    at org.springframework.webflow.engine.ViewState.resume(ViewState.java:196)
    at org.springframework.webflow.engine.Flow.resume(Flow.java:545)
    at org.springframework.webflow.engine.impl.FlowExecutionImpl.resume(FlowExecutionImpl.java:259)
    at org.springframework.webflow.executor.FlowExecutorImpl.resumeExecution(FlowExecutorImpl.java:169)
    at org.springframework.webflow.mvc.servlet.FlowHandlerAdapter.handle(FlowHandlerAdapter.java:183)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
    at org.springframework.security.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
    at org.springframework.security.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    at org.springframework.security.ui.SessionFixationProtectionFilter.doFilterHttp(SessionFixationProtectionFilter.java:67)
    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    at org.springframework.security.ui.ExceptionTranslationFilter.doFilterHttp(ExceptionTranslationFilter.java:101)
    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    at org.springframework.security.providers.anonymous.AnonymousProcessingFilter.doFilterHttp(AnonymousProcessingFilter.java:105)
    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    at org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter.doFilterHttp(SecurityContextHolderAwareRequestFilter.java:91)
    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    at org.springframework.security.ui.basicauth.BasicProcessingFilter.doFilterHttp(BasicProcessingFilter.java:174)
    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    at org.springframework.security.ui.AbstractProcessingFilter.doFilterHttp(AbstractProcessingFilter.java:278)
    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    at org.springframework.security.ui.logout.LogoutFilter.doFilterHttp(LogoutFilter.java:89)
    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    at org.springframework.security.context.HttpSessionContextIntegrationFilter.doFilterHttp(HttpSessionContextIntegrationFilter.java:235)
    at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    at org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:175)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:236)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

如何摆脱这种情况并使其发挥作用...我的控制器应该如何......

1 个答案:

答案 0 :(得分:0)

fEventsSet<FtpScanEvents>,根据您的要求,它肯定没有属性hostName

<form:input path="fEvents.hostName" ... />

如果要将集合绑定到Spring MVC表单,则需要迭代它并绑定每个项目,如下所示:

<c:forEach items="${fEvents}" var="event" varStatus="status" > 
    ... <form:input path="fEvents[${status.index}].hostName" ... /> ...
</c:forEach> 

但请注意you cannot use this approach with Set。您应该将fEvents的类型更改为List<FtpScanEvents>,或将表单绑定到对该属性使用List的DTO。