5

I'm new to Hibernate. I'm trying to run a simple query to see if the given login information for a user is correct:

public boolean checkLogin(String user, String pw)
{    
    pw = Util.encrypt(pw);

    String sql = "select count(*) from User where email = :email and pw = :pw";
    Query q = HibernateUtil.getSession().createQuery(sql);

    q.setParameter("email", user);
    q.setParameter("pw", pw);

    int count  = ( (Integer)  q.iterate().next() ).intValue();

    return count > 0;
}

When I call this method, I get an exception which says User class is not mapped.

To fixed this, I created this user class:

import javax.persistence.*;

@Entity @Table(name = "user")
public class User
{
    @Id @GeneratedValue
    @Column(name = "id")
    private Long id;

    public Long getId()
    {
        return id;
    }

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

And in my HibernateUtils.createSession I added the following line:

configuration.addClass(User.class);

However this gives me the exception:

Caused by: org.hibernate.MappingNotFoundException: resource: 
net/myProject/server/model/User.hbm.xml not found
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:741)
    at org.hibernate.cfg.Configuration.addClass(Configuration.java:786)
    at net.myProject.server.util.HibernateUtil.<clinit>(HibernateUtil.java:28)

What am I doing wrong?

5
  • please add the full stack trace Commented Feb 8, 2014 at 17:04
  • @CamiloBermúdez I added some more details Commented Feb 8, 2014 at 17:07
  • It looks like your hibernate isn't processing @Entity annotation... Can you show your persistence.xml file? Commented Feb 8, 2014 at 17:10
  • @MGorgon I don't have a persistence.xml file. Do I need to create one? I don't see any mention of it in the examples I've looked at. Commented Feb 8, 2014 at 17:12
  • Read docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/cfg/…, as well as the javadoc of the method just below. Also: createQuery() expects an HQL query, not an SQL query. count(*) is not valid HQL. And your variable should be named hql, not sql. Commented Feb 8, 2014 at 17:12

2 Answers 2

16

Use addAnnotatedClass instead of addClass

Configuration.addClass() indeed expects a companion hbm.xml

Sign up to request clarification or add additional context in comments.

7 Comments

Isn't that deprecated? mkyong.com/hibernate/…
Indeed, fixed my response
Thanks.. one more question. My id field is simply an auto_increment int column in a mysql table. What annotation do I need to use with it in order to get it to work in the same way as auto_increment?
@GeneratedValue(strategy = GenerationType.AUTO)
AUTO is the default, as the javadoc indicates: docs.oracle.com/javaee/6/api/javax/persistence/…. You can learn much by reading the javadoc, isn't it?
|
2

It seems that Hibernate is looking for your XML mapping (XML files), but you are using annotation mapping (Classes).

If you are using Hibernate with Java Persistence API you have to configure a persistence.xml file where you can inform where the mapped classes are.

(You don't need to have a User.hbm.xml file if you use JPA. But if you don't, you can declare your mappings for User in that XML file. You will also need to use a hibernate.cfg.xml to say where your mappings are.)

Here is an example of a persistence.xml file:

<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="PersistenceUnitName" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>package.name.YourEntityClass</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/database"/>
      <property name="javax.persistence.jdbc.user" value="user"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.password" value="pass"/>
    </properties>
  </persistence-unit>
</persistence>

5 Comments

Could you show me a sample of the persistence.xml file that I need to use for annotations?
And will the persistence.xml work with tomcat as well as jboss?
Yes. It's part of the JPA spec. If you have JPA support in your app you can use it. In case you are using an application server, you probably won't use transaction-type="RESOURCE_LOCAL" but "JTA" and will use a JNDI name instead of the 4 lines I used above to configure the database.
It seems that using config.addAnnotatedClass fixed it without needing this file.
Yes. You can also use the API programmatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.