如何使用hibernate注释+ Spring MVC映射两个表(一个到多个)

时间:2013-11-21 14:27:20

标签: java hibernate jsp spring-mvc

1.我正在使用spring mvc + Hibernate 2.我使用两个表实现一个项目,它具有一对多映射,工作得很好 3.然后,我想使用另外2个表,他们也有一个到多个。我为此假设了同样的情况。但是给出一个错误,它似乎是配置错误,我是新的,任何人都可以帮助我

这是我的MODEL ##(MeetingTypes和Meetings有1对多)

package pearson.dashboard.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;



@Entity
public class MeetingTypes {
    @Id
    @Column
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int meetingTypeID;
    @Column
    private String typeName;
    @OneToMany(mappedBy = "meetingTypes",fetch = FetchType.EAGER)

    private List<Meetings> meetings;
    public List<Meetings> getMeetings() {
        return meetings;
    }

    public void setMeetings(List<Meetings> meetings) {
        this.meetings = meetings;
    }

    public MeetingTypes() {

        // TODO Auto-generated constructor stub
    }

    public MeetingTypes(int meetingTypeID, String typeName
            ) {
        super();
        this.meetingTypeID = meetingTypeID;
        this.typeName = typeName;

    }

    public int getMeetingTypeID() {
        return meetingTypeID;
    }

    public void setMeetingTypeID(int meetingTypeID) {
        this.meetingTypeID = meetingTypeID;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }





}
package pearson.dashboard.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Meetings {
    @Id
    @Column
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int meetingID;
    @Column
    private Date sheduleTime;
    @Column
    private String meetingHeading;
    @Column 
    private String comment;
    @Column
    private String roomName;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "meetingTypeID")

    private MeetingTypes meetingTypes;

    public MeetingTypes getMeetingTypes() {
        return meetingTypes;
    }

    public void setMeetingTypes(MeetingTypes meetingTypes) {
        this.meetingTypes = meetingTypes;
    }

    public Meetings() {

        // TODO Auto-generated constructor stub
    }

    public Meetings(int meetingID, Date sheduleTime, String meetingHeading,
            String comment, String roomName) {
        super();
        this.meetingID = meetingID;
        this.sheduleTime = sheduleTime;
        this.meetingHeading = meetingHeading;
        this.comment = comment;
        this.roomName = roomName;

    }

    public int getMeetingID() {
        return meetingID;
    }

    public void setMeetingID(int meetingID) {
        this.meetingID = meetingID;
    }

    public Date getSheduleTime() {
        return sheduleTime;
    }

    public void setSheduleTime(Date sheduleTime) {
        this.sheduleTime = sheduleTime;
    }

    public String getMeetingHeading() {
        return meetingHeading;
    }

    public void setMeetingHeading(String meetingHeading) {
        this.meetingHeading = meetingHeading;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getRoomName() {
        return roomName;
    }

    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }




}

控制器##

的这一部分
@Controller
public class MeetingTypesController {
    @Autowired
    private MeetingTypeService  meetingTypeService;
    @Autowired
    private MeetingService meetingService;
    @RequestMapping("/detailsPage")
    public String getAllmeeting(Map<String, Object> map){
        map.put("meetingList", meetingTypeService.getAllMeetingTypes());

        return "details";

    }

这是我的DAO ##

部分
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pearson.dashboard.dao.MeetingTypeDao;

@Transactional
@Repository
public class MeetingTypeDaoImpl implements MeetingTypeDao  {
    @Autowired
    private SessionFactory sessionFactory;


    public List getAllMeetingTypes() {
        // TODO Auto-generated method stub
        return sessionFactory.getCurrentSession().createQuery("from MeetingTypes").list();
    }

}

JSP中的这个VIEW ##() 当我只用拳头伸手可及它正在工作时,但当我同时运行时它会发出吼叫错误......这就是我之前做过的事情..工作

<c:forEach items="${meetingList}" var="meetingType">

        <td>  This from MeetingTypes:  ${meetingType.typeName}</td> 

                  <c:forEach items="meetingType.Meetings" var="meet">

        <td>  This from Meetings:   ${meet.roomName}   </td>        
                  </c:forEach>
</c:forEach>

这是我的错误##

Hibernate: select teams0_.teamID as teamID0_, teams0_.name as name0_ from Teams teams0_
Hibernate: select releases0_.teamID as teamID0_1_, releases0_.releaseID as releaseID1_, releases0_.releaseID as releaseID1_0_, releases0_.cabCall as cabCall1_0_, releases0_.orcleCode as orcleCode1_0_, releases0_.prodDate as prodDate1_0_, releases0_.releaseName as releaseN5_1_0_, releases0_.remarks as remarks1_0_, releases0_.rrr as rrr1_0_, releases0_.staging as staging1_0_, releases0_.status as status1_0_, releases0_.teamID as teamID1_0_ from Releases releases0_ where releases0_.teamID=?
Hibernate: select releases0_.teamID as teamID0_1_, releases0_.releaseID as releaseID1_, releases0_.releaseID as releaseID1_0_, releases0_.cabCall as cabCall1_0_, releases0_.orcleCode as orcleCode1_0_, releases0_.prodDate as prodDate1_0_, releases0_.releaseName as releaseN5_1_0_, releases0_.remarks as remarks1_0_, releases0_.rrr as rrr1_0_, releases0_.staging as staging1_0_, releases0_.status as status1_0_, releases0_.teamID as teamID1_0_ from Releases releases0_ where releases0_.teamID=?
Hibernate: select releases0_.teamID as teamID0_1_, releases0_.releaseID as releaseID1_, releases0_.releaseID as releaseID1_0_, releases0_.cabCall as cabCall1_0_, releases0_.orcleCode as orcleCode1_0_, releases0_.prodDate as prodDate1_0_, releases0_.releaseName as releaseN5_1_0_, releases0_.remarks as remarks1_0_, releases0_.rrr as rrr1_0_, releases0_.staging as staging1_0_, releases0_.status as status1_0_, releases0_.teamID as teamID1_0_ from Releases releases0_ where releases0_.teamID=?
Hibernate: select releases0_.teamID as teamID0_1_, releases0_.releaseID as releaseID1_, releases0_.releaseID as releaseID1_0_, releases0_.cabCall as cabCall1_0_, releases0_.orcleCode as orcleCode1_0_, releases0_.prodDate as prodDate1_0_, releases0_.releaseName as releaseN5_1_0_, releases0_.remarks as remarks1_0_, releases0_.rrr as rrr1_0_, releases0_.staging as staging1_0_, releases0_.status as status1_0_, releases0_.teamID as teamID1_0_ from Releases releases0_ where releases0_.teamID=?
Hibernate: select releases0_.teamID as teamID0_1_, releases0_.releaseID as releaseID1_, releases0_.releaseID as releaseID1_0_, releases0_.cabCall as cabCall1_0_, releases0_.orcleCode as orcleCode1_0_, releases0_.prodDate as prodDate1_0_, releases0_.releaseName as releaseN5_1_0_, releases0_.remarks as remarks1_0_, releases0_.rrr as rrr1_0_, releases0_.staging as staging1_0_, releases0_.status as status1_0_, releases0_.teamID as teamID1_0_ from Releases releases0_ where releases0_.teamID=?
Hibernate: select releases0_.teamID as teamID0_1_, releases0_.releaseID as releaseID1_, releases0_.releaseID as releaseID1_0_, releases0_.cabCall as cabCall1_0_, releases0_.orcleCode as orcleCode1_0_, releases0_.prodDate as prodDate1_0_, releases0_.releaseName as releaseN5_1_0_, releases0_.remarks as remarks1_0_, releases0_.rrr as rrr1_0_, releases0_.staging as staging1_0_, releases0_.status as status1_0_, releases0_.teamID as teamID1_0_ from Releases releases0_ where releases0_.teamID=?
Hibernate: select releases0_.teamID as teamID0_1_, releases0_.releaseID as releaseID1_, releases0_.releaseID as releaseID1_0_, releases0_.cabCall as cabCall1_0_, releases0_.orcleCode as orcleCode1_0_, releases0_.prodDate as prodDate1_0_, releases0_.releaseName as releaseN5_1_0_, releases0_.remarks as remarks1_0_, releases0_.rrr as rrr1_0_, releases0_.staging as staging1_0_, releases0_.status as status1_0_, releases0_.teamID as teamID1_0_ from Releases releases0_ where releases0_.teamID=?
Hibernate: select releases0_.teamID as teamID0_1_, releases0_.releaseID as releaseID1_, releases0_.releaseID as releaseID1_0_, releases0_.cabCall as cabCall1_0_, releases0_.orcleCode as orcleCode1_0_, releases0_.prodDate as prodDate1_0_, releases0_.releaseName as releaseN5_1_0_, releases0_.remarks as remarks1_0_, releases0_.rrr as rrr1_0_, releases0_.staging as staging1_0_, releases0_.status as status1_0_, releases0_.teamID as teamID1_0_ from Releases releases0_ where releases0_.teamID=?
Hibernate: select meetingtyp0_.meetingTypeID as meetingT1_2_, meetingtyp0_.typeName as typeName2_ from MeetingTypes meetingtyp0_
Hibernate: select meetings0_.meetingTypeID as meetingT6_2_1_, meetings0_.meetingID as meetingID1_, meetings0_.meetingID as meetingID3_0_, meetings0_.comment as comment3_0_, meetings0_.meetingHeading as meetingH3_3_0_, meetings0_.meetingTypeID as meetingT6_3_0_, meetings0_.roomName as roomName3_0_, meetings0_.sheduleTime as sheduleT5_3_0_ from Meetings meetings0_ where meetings0_.meetingTypeID=?
Hibernate: select meetings0_.meetingTypeID as meetingT6_2_1_, meetings0_.meetingID as meetingID1_, meetings0_.meetingID as meetingID3_0_, meetings0_.comment as comment3_0_, meetings0_.meetingHeading as meetingH3_3_0_, meetings0_.meetingTypeID as meetingT6_3_0_, meetings0_.roomName as roomName3_0_, meetings0_.sheduleTime as sheduleT5_3_0_ from Meetings meetings0_ where meetings0_.meetingTypeID=?
Nov 21, 2013 7:34:07 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
javax.el.PropertyNotFoundException: Property 'roomName' not found on type java.lang.String
    at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:237)
    at javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:214)
    at javax.el.BeanELResolver.property(BeanELResolver.java:325)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:85)
    at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
    at org.apache.jsp.WEB_002dINF.views.details_jsp._jspx_meth_c_005fforEach_005f1(details_jsp.java:750)
    at org.apache.jsp.WEB_002dINF.views.details_jsp._jspx_meth_c_005fforEach_005f0(details_jsp.java:707)
    at org.apache.jsp.WEB_002dINF.views.details_jsp._jspService(details_jsp.java:256)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

Nov 21, 2013 7:34:07 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/controller] threw exception [javax.el.PropertyNotFoundException: Property 'roomName' not found on type java.lang.String] with root cause
javax.el.PropertyNotFoundException: Property 'roomName' not found on type java.lang.String
    at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:237)
    at javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:214)
    at javax.el.BeanELResolver.property(BeanELResolver.java:325)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:85)
    at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
    at org.apache.jsp.WEB_002dINF.views.details_jsp._jspx_meth_c_005fforEach_005f1(details_jsp.java:750)
    at org.apache.jsp.WEB_002dINF.views.details_jsp._jspx_meth_c_005fforEach_005f0(details_jsp.java:707)
    at org.apache.jsp.WEB_002dINF.views.details_jsp._jspService(details_jsp.java:256)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

在浏览器####

HTTP状态500 - javax.el.PropertyNotFoundException:在类型java.lang.String上找不到属性'roomName'

输入例外报告

消息javax.el.PropertyNotFoundException:在类型java.lang.String上找不到属性'roomName'

说明服务器遇到内部错误,导致无法完成此请求。

例外

1 个答案:

答案 0 :(得分:2)

我在jsp中看到一个错误:meetingType.Meetings在第二个forEach应该是${meetingType.Meetings}。 这是更正后的版本:

<c:forEach items="${meetingList}" var="meetingType">
    <td>  This from MeetingTypes:  ${meetingType.typeName}</td> 
    <c:forEach items="${meetingType.Meetings}" var="meet">
        <td>  This from Meetings:   ${meet.roomName}   </td>        
    </c:forEach>
</c:forEach>