是一个用户登录界面:
applicationContext.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-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="packagesToScan">
<list>
<value>org</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${dialect}</prop>
<prop key="hibernate.show_sql">${show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hbm2ddl_auto}</prop>
<prop key="hibernate.format_sql">${format_sql}</prop>
</props>
</property>
</bean>
<bean id="login" class="org.LoginAction">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>
User.hbm.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="org.User" table="user" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="Id"/>
            <generator class="native"/>
        </id>
        <property name="username" type="java.lang.String">
            <column name="username" length="10" not-null="true"/>
        </property>
        <property name="password" type="java.lang.String">
            <column name="password" length="10" not-null="true"/>
        </property>
    </class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration> <session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">mysql</property>
<mapping resource="org/User.hbm.xml"/> </session-factory></hibernate-configuration>
下面是工程目录登录界面是有的,输入用户名和密码之后就出现
org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [from User u where u.username=? and u.password=?]错误了,求解怎么解决,弄了好半天了,快崩溃了

解决方案 »

  1.   

    把hbm.xml文件和你的实体类放在一起
      

  2.   

    是放到和LoginAction同一个包下?还是??
      

  3.   

    和你的User类放在一起。
    比如你的类:  com/lz/entity/User.java
    你的hbm.xml:com/lz/entity/User.hbm.xml
      

  4.   

    拷此hibernate的原文档过来,LZ看一下package org.hibernate.tutorial.domain;public class Person {    private Long id;
        private int age;
        private String firstname;
        private String lastname;    public Person() {}    // Accessor methods for all properties, private setter for 'id'}
    Save this to a file named src/main/java/org/hibernate/tutorial/domain/Person.javaNext, create the new mapping file as src/main/resources/org/hibernate/tutorial/domain/Person.hbm.xml
      

  5.   

    hql语句贴上来看看。非常可能错了
      

  6.   

    String hql="from User u where u.username=? and u.password=?";
      

  7.   

    你的实体类呢User, 没有建立吗看看这个
    http://blog.csdn.net/edward_qing_lee/article/details/7753030
      

  8.   

    你的实体没交给hibernate你用了spring那就要在配置数据源那加载你的 实体映射文件
      

  9.   

    你的映射文件User.hbm.xml里指定了表user映射的实体类是org.User类,但是我没看到你的工程里有这个类啊,是不是这个原因出错了?
      

  10.   

    package org;import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    @Entity
    @Table(name="user")   //指定映射的表名
    public class User {
     
     private int id;
     private String username;
     private String password; @Id
     @GeneratedValue
     public int getId() {
      return id;
     }
    }
      

  11.   

    这样我建议你还是先用xml的方式写你的实体bean,先不要用annotation方式,如果问题解决在换成annotation方式,我怀疑是你的xml中的配置的问题!
      

  12.   

    加上后就找不到了,  The requested resource (/ssh/login.jsp) is not available.
      

  13.   

    你这个是报的找不到JSP文件,要看你发布的目录是怎样的了
      

  14.   

    我的意思是,你把TOMCAT下,工程发布后的目录结构发来看看
      

  15.   

    在Spring配置文件中加入
    <property name="mappingLocations">
         <list>
    <value>classpath:org/User.hbm.xml</value>

    </list>
    </property>