问题描述
最近在学习openJPA,做的小demo老是报错,请教一下,代码如下;persistence.xml<?xmlversion="1.0"encoding="UTF-8"?><persistencexmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistencehttp://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"version="1.0"><persistence-unitname="demo"transaction-type="RESOURCE_LOCAL"><provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider><properties><propertyname="openjpa.ConnectionURL"value="jdbc:mysql://localhost:3306/openjpa"/><propertyname="openjpa.ConnectionDriverName"value="com.mysql.jdbc.Driver"/><propertyname="openjpa.ConnectionUserName"value="root"/><propertyname="openjpa.ConnectionPassword"value="root"/><propertyname="openjpa.RuntimeUnenhancedClasses"value="supported"/></properties></persistence-unit></persistence>
web.xml<?xmlversion="1.0"encoding="UTF-8"?><web-appversion="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!--加载Spring配置文件--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!--Spring容器监听器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--配置SpringMVC模块--><servlet><servlet-name>sjdemo</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>sjdemo</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping></web-app>
sjdemo-servlet.xml<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!--启用springAnnotation--><context:annotation-config/><!--扫描指定的包,以完成自动注入--><context:component-scanbase-package="com.jzkangta.sjd"></context:component-scan><!--启动springMVC注解--><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/><!--给模型视图添加前后缀--><beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyname="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property><propertyname="prefix"><value>/WEB-INF/pages/</value></property><propertyname="suffix"><value>.jsp</value></property></bean><!--开始整合JPA--><beanid="entityManagerFactory"class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"><propertyname="persistenceUnitName"value="demo"/></bean><!--JPA事务管理器--><beanid="transactionManager"class="org.springframework.orm.jpa.JpaTransactionManager"><propertyname="entityManagerFactory"ref="entityManagerFactory"/></bean><tx:annotation-driventransaction-manager="transactionManager"/></beans>
Person类packagecom.jzkangta.sjd.entity;importjava.io.Serializable;importjavax.persistence.*;@Entity@Table(name="person",catalog="openjpa")publicclassPersonimplementsSerializable{@Id@GeneratedValue(strategy=GenerationType.TABLE)privateintid;@Column(name="personName",unique=false,nullable=false,length=50)privateStringname;@Column(name="password",unique=false,nullable=false,length=50)privateStringpassword;publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}}
packagecom.jzkangta.sjd.dao.impl;importjava.util.List;importjavax.persistence.EntityManager;importjavax.persistence.PersistenceContext;importjavax.persistence.Query;importorg.springframework.stereotype.Component;importorg.springframework.transaction.annotation.Propagation;importorg.springframework.transaction.annotation.Transactional;importcom.jzkangta.sjd.dao.PersonDAO;importcom.jzkangta.sjd.entity.Person;@Component//告诉spring这个类不需要配置@Transactional//告诉spring这个类是需要事务管理的publicclassPersonDaoImplimplementsPersonDAO{@PersistenceContext//通过注解来获得emEntityManagerem;publicbooleancheckLogin(Stringname,Stringpassword){booleanflag=false;Queryquery=em.createQuery("selectofromPersonowhereo.personName=?1ando.password=?2");query.setParameter(1,name);query.setParameter(2,password);List<Person>list=query.getResultList();if(list.size()>0){flag=true;}returnflag;}}
调用checkLogin报如下错:严重:Servlet.service()forservletsjdemothrewexception<openjpa-1.2.1-r752877:753278nonfatalusererror>org.apache.openjpa.persistence.ArgumentException:Anerroroccurredwhileparsingthequeryfilter"selectofromPersono".Errormessage:Thename"Person"isnotarecognizedentityoridentifier.Knownentitynames:[]atorg.apache.openjpa.kernel.exps.AbstractExpressionBuilder.parseException(AbstractExpressionBuilder.java:118)atorg.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getClassMetaData(JPQLExpressionBuilder.java:180)atorg.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.resolveClassMetaData(JPQLExpressionBuilder.java:150)atorg.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getCandidateMetaData(JPQLExpressionBuilder.java:225)atorg.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getCandidateMetaData(JPQLExpressionBuilder.java:195)atorg.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.getCandidateType(JPQLExpressionBuilder.java:188)atorg.apache.openjpa.kernel.jpql.JPQLExpressionBuilder.access$600(JPQLExpressionBuilder.java:69)atorg.apache.openjpa.kernel.jpql.JPQLExpressionBuilder$ParsedJPQL.populate(JPQLExpressionBuilder.java:1756)atorg.apache.openjpa.kernel.jpql.JPQLParser.populate(JPQLParser.java:56)atorg.apache.openjpa.kernel.ExpressionStoreQuery.populateFromCompilation(ExpressionStoreQuery.java:153)atorg.apache.openjpa.kernel.QueryImpl.newCompilation(QueryImpl.java:658)
不知道是何原因。。。
解决方案
解决方案二:
该回复于2010-04-25 08:42:34被版主删除
解决方案三:
Person对象是不是需要带参数的构造函数...........