org.hibernate.hql.ast.QuerySyntaxException

时间:2011-01-10 10:42:57

标签: java hibernate

org.hibernate.hql.ast.QuerySyntaxException:
     

用户未映射[SELECT email,id   来自用户WHERE email='dsdd@dds.com'   AND password ='asasas']

public ILogin authenticate(Login login) {
        System.out.println(login);
        System.out.println(login.getEmail());
        String query = "SELECT email, id FROM users WHERE email='"
        + login.getEmail() + "' AND password='" + login.getPassword() + "'";
        results = getHibernateTemplate().find(query);
        System.out.println(results);
        return null;
}

我有一个Login Bean类......接下来是。

package 

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class Login {
      public Login(){}
        private Long id = null;
        private String email;
        private String password;

        public Login(String email, String password)
        {
            this.email = email;
            this.password = password;
        }

        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;

        }
}

我的application-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" default-autowire="byName" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
  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">

<!-- Turn on AspectJ @Configurable support -->

<context:spring-configured />
<context:property-placeholder location="classpath*:*.properties" />
<context:component-scan base-package="com.intermedix"/>
<context:annotation-config/>

<!-- Turn on @Autowired, @PostConstruct etc support -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.intermedix.domain.Login</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/spring"/>
        <property name="username" value="monty"/>
        <property name="password" value="indian"/>
    </bean>   

    <tx:annotation-driven transaction-manager="txManager"/>

    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

5 个答案:

答案 0 :(得分:8)

即使它不属于您的问题:

不要以这种方式使用Hibernate / JPA(String concatination)!

String query = "SELECT email, id FROM users AS u WHERE email='"+ login.getEmail() + "' AND password='" + login.getPassword() + "'";

而是使用类似预准备语句的HQL:

createQuery(
   "SELECT l FROM login WHERE l.email=:email AND l.password=:password")
   .setParameter("login",login.getEmail())
   .setParameter("password",login.getPassword());

如果你以“你的风格”这样做,那么SQL注入会有很多乐趣!

下一篇:阅读关于HQL的hibernate参考资料,对我而言,看起来你正在编写SQL而不是HQL。

答案 1 :(得分:5)

  

SELECT email,id FROM users

什么是“用户”?您的配置或代码中没有任何内容称为“用户”,因此Hibernate不知道您在说什么。

其次,您的Login类未使用@Entity注释,因此Hibernate可能会忽略它。

因此添加注释,并且很可能将您的查询更改为:

  

SELECT email,id FROM Login

答案 2 :(得分:2)

对我来说很明显:“用户未映射......”

您没有映射Users表,或者您没有错误地配置它。

答案 3 :(得分:0)

@Entity
@Table(name="users")
public class Login {

您实际上需要注释Login类,因为您在application-context.xml中说(<property name="annotatedClasses">),例如这样。

@Column(name="password")
public String getPassword() {
            return password;
        }

答案 4 :(得分:0)

List list = getHibernateTemplate().find("from Form3A where FAC_ID=?",FAC_ID);

HERE Form3A是类的名称,配置文件是

<property name="annotatedClasses">
<list>
<value>org.fbis.models.Form3A</value>
</list>
</property>