本文介绍了为什么我的set变量在执行多次查询时抛出错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我是春天和休眠的新手。我已经创建了一个带有hibernate集成的spring-mvc应用程序,当我运行代码时,我得到下面给出的错误。我的要求是从给定客户类别的广告中选择相关路径。这涉及两个表和第三个表,用于许多映射。I am new to spring and hibernate. I have created a spring-mvc app with hibernate integration and when I run the code I get the below given error. My requirement is to pick the relevant path from the advertisement given the customer category. This involves two tables respectively along with the 3rd table for many-many mapping.org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernate3AnnotatedSessionFactory' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: customercategory, for columns: [org.hibernate.mapping.Column(advertisements)]org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629)org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677)org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548)org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)javax.servlet.GenericServlet.init(GenericServlet.java:158)org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2476)org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2465)java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)java.lang.Thread.run(Thread.java:745) 以下是我的模特课广告Below is my model class Advertisement@Entity@Table(name = "advertisement")public class Advertisement {@Id@Column(name="id")@GeneratedValue(strategy = GenerationType.IDENTITY)private int id;private String path;private String name;public int getId(){ return id;}public String getName(){ return name;}public void setName(){ this.name=name;}public String getPath(){ return path;}public void setPath(){ this.path=path;}} 以下是另一个型号customercategorybelow is another model customercategory@Entity@Table(name = "customercategory")public class CustomerCategory {@Id@Column(name="id")@GeneratedValue(strategy = GenerationType.IDENTITY)private int id;@Column(name = "name")private String name;private Set advertisements;public int getId(){ return id;}public String getName(){ return name;}public void setName(){ this.name=name;}public Set getAdvertisements(){ return advertisements;}public void setAdvertisements(Set advertisements){ this.advertisements=advertisements;}} 以下是我的DAO类,其中我试图运行查询这需要来自许多桌子的信息。Below is my DAO class in which im trying to run a query that needs information from a many-many table.public class CustomerCategoryDAOImpl implements CustomerCategoryDAO {private SessionFactory sessionFactory;private int catId;private String adPath;public void setSessionFactory(SessionFactory sessionFactory){ this.sessionFactory=sessionFactory;}@Overridepublic String getAdPath(String categoryName) { sessionFactory = new Configuration() .addResource("com/springapp/mvc/CustomerCategory.hbm.xml") .buildSessionFactory(); Session session=this.sessionFactory.openSession(); session.beginTransaction(); Query query=session.createQuery("from CustomerCategory where name=:categoryName"); query.setParameter("name",categoryName); for(Iterator it=query.iterate();it.hasNext();) { CustomerCategory c=(CustomerCategory)it.next(); catId=c.getId(); Set advertisements=c.getAdvertisements(); for(Iterator it2=advertisements.iterator();it2.hasNext();) { Advertisement ad=(Advertisement)it2.next(); adPath=ad.getPath(); } } session.getTransaction().commit(); session.close(); return adPath;}} 以下是我的控制器类,它响应网址请求。below is my controller class that responds to the url request.@Controller@RequestMapping(value = "/{category}", method = RequestMethod.GET)public class HelloController {@RequestMapping(method = RequestMethod.GET)public void printWelcome(@PathVariable("category") String categoryName, @RequestParam("value") String value,HttpServletResponse response) { ApplicationContext context = new FileSystemXmlApplicationContext ("classpath:/../mvc-dispatcher-servlet.xml"); CustomerCategoryDAO cDAO=context.getBean(CustomerCategoryDAO.class); String path = cDAO.getAdPath(value); try { //Get path to ad for the categoryName from the database File adFile = new File(path); // get your file as InputStream InputStream is = FileUtils.openInputStream(adFile); // copy it to response's OutputStream org.apache.commons.io.IOUtils.copy(is, response.getOutputStream()); response.flushBuffer(); } catch (IOException ex) { throw new RuntimeException("IOError writing file to output stream"); }}} 以下是我的调度员xml。Below is my dispatcher xml.<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.springapp.mvc"/><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/></bean><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/ccas" /> <property name="username" value="root" /> <property name="password" value="" /></bean><bean id="hibernate3AnnotatedSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="annotatedClasses"> <list> <value>com.springapp.mvc.CustomerCategory</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.current_session_context_class">thread</prop> <prop key="hibernate.show_sql">false</prop> </props> </property></bean><bean id="customerCategoryDAO" class="com.springapp.mvc.CustomerCategoryDAOImpl"> <property name="sessionFactory" ref="hibernate3AnnotatedSessionFactory" /></bean> 及以下是我的hibernate xml。and below is my hibernate xml.<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.springapp.mvc"><class name="CustomerCategory" table="customercategory"> <meta attribute="class-description"> This class contains the customer category detail. </meta> <id name="id" type="int" column="id"> <generator class="native"/> </id> <set name="advertisements" cascade="save-update" table="customercategory_has_advertisement"> <key column="customerCategory_id"/> <many-to-many column="advertisement_id" class="Advertisement"/> </set> <property name="name" column="name" type="string"/></class><class name="Advertisement" table="advertisement"> <meta attribute="class-description"> This class contains the advertisement records. </meta> <id name="id" type="int" column="id"> <generator class="native"/> </id> <property name="name" column="name" type="string"/> <property name="path" column="path" type="string"/></class>推荐答案 这篇关于为什么我的set变量在执行多次查询时抛出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-06 09:45