Spring3 + Hibernate3模型即使在一切设置正确

时间:2016-10-16 02:57:51

标签: spring hibernate

希望我做的一切正确,但即便如此,错误500显示我的模型未映射。请帮助我过去一周 我的表名是使用字段用户名和密码登录。

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns="http://java.sun.com/xml/ns/javaee"  
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" 
version="3.0">           <display-name>springhiber</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<display-name>SpringHiber</display-name>
<servlet>
<description></description>
<display-name>login</display-name>
<servlet-name>login</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-    
class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


<description>MySQL Spring Web</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/spring</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:com/ictc/spring/web/config/dao-context.xml
classpath:com/ictc/spring/web/config/service-context.xml
</param-value>

</context-param> 
</web-app>

登录-context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc  
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
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-4.2.xsd">
<context:component-scan
    base-package="com.ictc.spring.web.controllers">
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="pageViewResolver"      
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<mvc:resources location="/resources/" mapping="/static/*"></mvc:resources> 
<mvc:default-servlet-handler/>
</beans>

com.ictc.spring.web.config

道context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.ictc.spring.web.dao">    
</context:component-scan>
<jee:jndi-lookup jndi-name="jdbc/spring" id="dataSource"
    expected-type="javax.sql.DataSource">
</jee:jndi-lookup>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactor
yBean">
     <property name="dataSource" ref ="dataSource">
    </property>
    <property name="packagesToScan">
        <list>
            <value>
                com.ictc.spring.web.dao
            </value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQL5Dialect
            </prop>
        </props>
    </property>

</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource">
    </property>
</bean>
<tx:annotation-driven />
</beans>

服务context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<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-4.2.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan
    base-package="com.ictc.spring.web.service">
</context:component-scan>
</beans>

com.ictc.spring.web.dao

LoginData.java

package com.ictc.spring.web.dao;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;
@Entity
@Table(name="login")
public class LoginData {  
@Size(min=5,message="Username should be of more than 5 characters")
private String username;
@Size(min=8,message="Password should be atleast 8 characters long")
private String password;
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
}

LoginDao.java

package com.ictc.spring.web.dao;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@Component
public class LoginDao {
@Autowired
private SessionFactory sessionFactory;
public Session session()
{
    return sessionFactory.getCurrentSession();
}
public LoginDao()
{
    System.out.println("succesfully loaded");
}
@SuppressWarnings("unchecked")
public List<LoginData> getDetails()
{
    return session().createQuery("from LoginData").list();
}
public void create(LoginData log) {
     session().save(log);
}
}

栈跟踪

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.hql.ast.QuerySyntaxException: LoginData is not mapped [from LoginData]

例外报告     消息请求处理失败;嵌套异常是
    org.hibernate.hql.ast.QuerySyntaxException:LoginData未映射[来自     LoginData]

description The server encountered an internal error that prevented it from  
fulfilling this request.

例外

org.springframework.web.util.NestedServletException: Request processing 
failed; nested exception is org.hibernate.hql.ast.QuerySyntaxException: 
LoginData is not mapped [from LoginData]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkSer
vlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java
:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.ja
va:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
org.hibernate.hql.ast.QuerySyntaxException: LoginData is not mapped [from 
LoginData]
org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(Sessio
nFactoryHelper.java:180)
org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFact
ory.java:111)
org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:327)
org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3
441)
org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.ja
va:3325)
org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:73
3)
org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:584)
org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.ja
va:301)
org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244
)
org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:2
56)
org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java
:187)
org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:1
38)
org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.jav
a:124)
org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.j
ava:156)
org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:
135)
org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
com.ictc.spring.web.dao.LoginDao.getDetails(LoginDao.java:73)
com.ictc.spring.web.dao.LoginDao$$FastClassBySpringCGLIB$$791e7be8.invoke(<g
enerated>)
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invoke
Joinpoint(CglibAopProxy.java:720)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(Reflect
iveMethodInvocation.java:157)
org.springframework.transaction.interceptor.TransactionInterceptor$1.proceed
WithInvocation(TransactionInterceptor.java:99)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeW
ithinTransaction(TransactionAspectSupport.java:281)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(Tr
ansactionInterceptor.java:96)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(Reflect
iveMethodInvocation.java:179)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.in
tercept(CglibAopProxy.java:655)
com.ictc.spring.web.dao.LoginDao$$EnhancerBySpringCGLIB$$3d95e8ed.getDetails
(<generated>)
com.ictc.spring.web.service.LoginService.getCurrent(LoginService.java:24)
com.ictc.spring.web.controllers.LoginController.showLogin(LoginController.ja
va:72)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(Invoc
ableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForReque
st(InvocableHandlerMethod.java:136)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandle
rMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerA
dapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerA
dapter.handleInternal(RequestMappingHandlerAdapter.java:738)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.hand
le(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServl
et.java:963)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServle
t.java:897)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkSer
vlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java
:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.ja
va:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

2 个答案:

答案 0 :(得分:0)

让我们从右边开始配置您的调度程序servlet。它假设是这样的:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/a_folder_path/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/a_folder_path/dao-context.xml
        /WEB-INF/a_folder_path/service-context.xml
    </param-value>
</context-param>

一些事项:使用调度程序servlet的dispatcher名称。

将您的上下文文件放在WEB-INF

一个更重要的子弹: 更改login-context.xml文件的名称

dispatcher-servlet.xml

还有:

从web.xml中删除此代码,这不是您配置数据源的方式:

<description>MySQL Spring Web</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/spring</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

答案 1 :(得分:0)

感谢上帝,Atlast我得到了解决方案 我正在使用Spring-orm 3.2.3和hibernate 3.6.10 Final.I遵循基于注释的方法。我去了几个建议但是一切都出错了。但是在1周之后,我自己得到了解决方案。 问题在于sessionFactory bean的packagesToScan属性。该属性不适用于iam使用的版本。相反,我使用带注释的类属性指向我的模型 spring-orm和hibernate-core jar也需要javassist 3.12.1 GA jar。 在我的情况下,最后但至少@Id注释必须用于指向主键。 感谢所有试图帮助我的人