Hibernate MappingNotFoundException when using annotations to map an Entity class

hibernate, java

Solution

Use `addAnnotatedClass` instead of `addClass`

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

Problem

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?

Original source