使用带有hibernate和spring的命名查询时遇到问题。
下面是我的代码和堆栈跟踪。
调用我的查询的方法是:
@SuppressWarnings("unchecked")
@Override
public List<CampaignEntity> getCampaignList() {
final Query clientQuery = getNamedQuery(CampaignEntity.GET_CAMPAIGNS_QUERY);
return (List<CampaignEntity>) clientQuery.list();
}
我的实体类是
package com.nativeunlock.dto;
import java.io.Serializable;
import javax.persistence.*;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@SuppressWarnings("serial")
@NamedQueries({
@NamedQuery(
name = CampaignEntity.GET_CAMPAIGNS_QUERY,
query = "from CampaignEntity campaign"
)
})
@Entity
@Data
@Table(name = "campaign")
public class CampaignEntity implements Serializable {
public static final String GET_CAMPAIGNS_QUERY ="getCampaignList";
@Id
@Getter
@Setter
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "campaingn_id", unique = true, nullable = false)
private int campain_id;
@Getter
@Setter
@Column(name = "no_of_views")
private int no_of_views;
@Getter
@Setter
@Column(name = "video_url")
private int video_url;
}
我的xml文件是
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<context:component-scan base-package="com.nativeunlock.**" />
<context:property-placeholder location="classpath:common.properties" />
<bean id="dataSource" class="${datasource.class}">
<property name="driverClassName" value="${mysql.driver}" />
<property name="url" value="${mysql.url}" />
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.nativeunlock.dao.**"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="savedRequestAwareAuthenticationSuccessHandler"
class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<property name="targetUrlParameter" value="targetUrl" />
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<security:global-method-security
jsr250-annotations="enabled" pre-post-annotations="enabled"
secured-annotations="enabled" />
</beans>
我的stacktrace是
org.hibernate.MappingException: Named query not known: getCampaignList
at org.hibernate.internal.AbstractSessionImpl.getNamedQuery(AbstractSess
ionImpl.java:154)
at org.hibernate.internal.SessionImpl.getNamedQuery(SessionImpl.java:137
1)
at com.nativeunlock.dao.CampaignDao.getNamedQuery(CampaignDao.java:41)
at com.nativeunlock.dao.CampaignDao.getCampaignList(CampaignDao.java:47)
at com.nativeunlock.service.CampaignService.getCampaignList(CampaignServ
ice.java:20)
at com.nativeunlock.security.controller.LoginController.campaign(LoginCo
ntroller.java:31)
at com.nativeunlock.security.controller.LoginController$$FastClassByCGLI
B$$25d5f75b.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocatio
n.invokeJoinpoint(Cglib2AopProxy.java:689)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:150)
at org.springframework.security.access.intercept.aopalliance.MethodSecur
ityInterceptor.invoke(MethodSecurityInterceptor.java:64)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterc
eptor.intercept(Cglib2AopProxy.java:622)
at com.nativeunlock.security.controller.LoginController$$EnhancerByCGLIB
$$ac304978_2.campaign(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.
invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandle
rAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandle
rAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(Dispatch
erServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(Dispatche
rServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(Frame
workServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServl
et.java:778)
任何人都可以帮我找出我做错了什么吗?
答案 0 :(得分:0)
您似乎没有扫描您命名查询所在的软件包,而是扫描com.nativeunlock.dao.**
<property name="packagesToScan" value="com.nativeunlock.dao.**"/>
您的实体位于com.nativeunlock.dto