Antonio Argentieri
Antonio Argentieri

Reputation: 167

Table is not mapped

I need help with my simple servlet in Java. I have a Login form (email and password fields) in HTML file, a Java servlet and a utility for the login in witch is checked if into de database exists a couple with the same email and password. Running the program I have a Syntax Error with my query, the error says that "User is not mapped". I think that this is a dummy error, could anyone help me?

Here the model.User class

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table( name = "USERS",
        uniqueConstraints = {@UniqueConstraint(columnNames = {"email"})})

public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="user_id", nullable=false, unique=true)
    private int id;

    @Column(name="firstName", length=40, nullable=false)
    private String firstName;

    @Column(name="lastname", length=40, nullable=false)
    private String lastName;

    @Column(name="email", length=40, nullable=false)
    private String email;

    @Column(name="password", length=40, nullable=false)
    private String password;

    public User(String firstName, String lastName, String email, String password){
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
    }

    public int getId(){
        return this.id;
    }

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

    public String getFirstName(){
        return this.firstName;
    }

    public void setFirstName(String firstName){
        this.firstName = firstName;
    }

    public String getLastName(){
        return this.lastName;
    }

    public void setLastName(String lastName){
        this.lastName = lastName;
    }

    public String getEmail(){
        return this.email;
    }

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

    public String getPassword(){
        return this.password;
    }

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

Here the utility.LoginDao class

public static boolean validateCredentials(String email, String password){
        Session session = DbService.getSession();

        session.beginTransaction();

        Query query = session.createQuery
                ("from Users u where u.email=:email and u.password=:password");

        query.setParameter("email", email);
        query.setParameter("password", password);

        List list=query.list();
                ...

Here my LoginServlet doPost() method:

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {  
        String email = request.getParameter("email");  
        String password = request.getParameter("password");

        if(LoginDao.validateCredentials(email, password)){
            RequestDispatcher view = request.getRequestDispatcher("SigninSuccess.jsp");
            view.forward(request, response);
        }
        else{  
            RequestDispatcher rd=request.getRequestDispatcher("index.html");  
            rd.include(request,response);  
        }  
    }

Here signin.html form file

<form method="POST" action="LoginServlet">
            <p>
                <label for="fname">E-mail</label>
                <input type="text" name="email" placeholder="e-mail">
            </p>
            <p>
                <label for="lname">Password</label>
                <input type="password" name="password" placeholder="password">
            </p>        
            <input type="submit" value="Login">
            <p class="message">Not registered? <a href="./signup.html">Create an account</a></p>
        </form>

this is the error:

User is not mapped [from User u where u.email=:email and p.password=:password] at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157) ...

Upvotes: 1

Views: 401

Answers (3)

Antonio Argentieri
Antonio Argentieri

Reputation: 167

Finally I have solved my problem. I forgotten to add config.addAnnotatedClass(model.User.class); in my configuration in LoginDao class. Thanks all.

Upvotes: 0

Captain
Captain

Reputation: 744

Razib gave you the correct answer in telling you to use User in your query rather than Users. If you are still getting the same error, then it can only be because Hibernate has not found the @Entity class.

Please run the following code and respond with the output.

sessionFactory.getConfiguration()
                .getClassMappings()
                .forEachRemaining(pc -> System.out.println(pc.getEntityName() + "\t" + pc.getTable().getName()));

hint: To get the sessionFactory, just autowire it in a service or a dao:

@Autowired
private LocalSessionFactoryBean sessionFactory;

Upvotes: 0

Razib
Razib

Reputation: 11153

Here you are creating Query using the table name (users) in the database -

Query query = session.createQuery
                ("from Users u where u.email=:email and u.password=:password");

That's why you are getting the error. You need to use the entity name - User -

Query query = session.createQuery
                ("from User u where u.email=:email and u.password=:password");  

HQL expect you are using java class name and java property name. You also have to care that you are using javax.persistence.Entity not org.hibernate.annotations.Entity

Upvotes: 2

Related Questions