Struts 2 Hibernate

时间:2013-12-04 05:25:38

标签: hibernate struts2 hibernate-mapping

我正在学习Struts2和Hibernate ......

我有一个客户类,其中包含与每个实例关联的地理代码。当我在JSP上显示时,我需要用标签分解每个字符。

    String geoCode = "1324567890";

需要成为

    List<String> geoCodeList = "[1][2][3]..."

这样我就可以在JSP中使用一个子集。

我尝试做的是向Customer.java对象添加另一个属性。我有以下内容:

private String geoCode;
private List<String> geoCodeList;

@Column(name = "GEO_CD")
public String getGeoCd() {
    return geoCd;
}

public void setGeoCd(String geoCd) {
    this.geoCd = geoCd;

    geoCdList = new ArrayList<String>();

    System.out.println("geo length = " + geoCd.length());
    for (int i=0; i < geoCd.length(); i++) {
        geoCdList.add(geoCd.substring(i,i+1));          
    }
}

这一切都很好但是一旦我为geoCdList添加了一个getter方法,我就会收到错误。

public List<String> getGeoCdList() {
    return geoCdList;
}

错误是:

    Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine     type for: java.util.List, at table: Customer, for columns: [org.hibernate.mapping.Column(geoCdList)]
    Dec 03, 2013 10:21:26 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [default] in context with path [/msps-web] threw exception  [Filter execution threw an exception] with root cause
    org.hibernate.MappingException: Could not determine type for: java.util.List, at table: Customer, for columns: [org.hibernate.mapping.Column(geoCdList)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290)
at org.hibernate.mapping.Property.isValid(Property.java:243)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:464)
at org.hibernate.mapping.RootClass.validate(RootClass.java:235)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
at com.att.msps.database.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
at com.att.msps.database.util.HibernateUtil.<clinit>(HibernateUtil.java:8)
at com.att.msps.customer.view.CustomerAction.<init>(CustomerAction.java:28)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:374)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:131)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:162)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:151)
at com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:121)
at com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:297)
at com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:397)
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:194)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:553)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
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: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:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
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:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)

我的猜测是,它认为这是桌上的一个栏目,但我不太确定。任何帮助将不胜感激。

感谢!!!!!

3 个答案:

答案 0 :(得分:1)

如果要将geoCodeList保留到数据库,则需要使用@ElementCollection注释将其映射为字符串集合。

@ElementCollection
private List<String> geoCodeList;

但是如果您不想保留列表,那么您需要将列表创建逻辑从setter方法移动到其他方法,并使用@Transient注释注释此方法。

答案 1 :(得分:1)

在实体和实体中保留一个列表并不是一个好主意。不要将其映射到数据库中。

无论如何,您可以在该字段上使用@Transient注释&amp;它不会被坚持下去。

答案 2 :(得分:0)

不要为geoCdList添加任何注释。