theJava
theJava

Reputation: 15034

org.hibernate.hql.ast.QuerySyntaxException

org.hibernate.hql.ast.QuerySyntaxException:

users is not mapped [SELECT email, id FROM users WHERE email='[email protected]' 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;
}

I have a Login Bean class... here it follows.

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;

        }
}

My 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>

Upvotes: 3

Views: 12854

Answers (5)

Nagappa L M
Nagappa L M

Reputation: 1480

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

HERE Form3A is name of class and config file is

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

Upvotes: 0

user1701556
user1701556

Reputation: 47

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

You actually need to annotate Login class since you say (<property name="annotatedClasses">) that in application-context.xml for example like this.

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

Upvotes: 0

Ralph
Ralph

Reputation: 120781

Even if it does not belong to your question:

Don't use Hibernate/JPA in this way (String concatination) !:

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

Instead use HQL like prepared statements:

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

If you do it in "your style" you will have lot of fun with SQL Injections!

Next: read the hibernate reference about HQL, for me it looks like you are writing SQL instead of HQL.

Upvotes: 8

darioo
darioo

Reputation: 47183

Looks obvious to me : "users is not mapped..."

Either you didn't map the Users table, or you have it incorrectly configured.

Upvotes: 2

skaffman
skaffman

Reputation: 403481

SELECT email, id FROM users

What is "users"? There is nothing in your config or code called "users", so Hibernate has no idea what you're talking about.

Secondly, your Login class is not annotated with @Entity, so Hibernate is likely ignoring it.

So add the annotation, and most likely change your query to:

SELECT email, id FROM Login

Upvotes: 5

Related Questions