I had the same problem using Dropwizard and Hibernate. I had a User entity class like this:
@Entity
@Table(name = "users")
@NamedQueries({
@NamedQuery(name = "com.majidkhorsandi.dropbookmarks.core.User.findAll",
query = "SELECT u FROM User u"),
@NamedQuery(name = "com.majidkhorsandi.dropbookmarks.core.User.findByUsernamePassword",
query = "SELECT u FROM User u WHERE u.username = :username AND u.password = :password")
})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String username;
private String password;
/*private List<Bookmark> bookmarks = new ArrayList<>();*/
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
...
And then I had my DAO class as:
public class UserDAO extends AbstractDAO<User> {
UserDAO(SessionFactory sessionFactory) {
super(sessionFactory);
}
List findAll() {
return list(namedQuery("com.majidkhorsandi.dropbookmarks.core.User.findAll"));
}
public Optional<User> findByUsernamePassword(String username, String password) {
return Optional.ofNullable(uniqueResult(
namedQuery("com.majidkhorsandi.dropbookmarks.core.User.findByUsernamePassword")
.setParameter("username", username)
.setParameter("password", password)
));
}
}
Then here is how my hibernate.cfg.xml looked:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1</property>
<property name="connection.username">sa</property><!--./target/example-->
<property name="connection.password">sa</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Enable Hibernate's current sessions tracking by thread of execution -->
<property name="current_session_context_class">managed</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping class="com.majidkhorsandi.dropbookmarks.core.User"/>
</session-factory>
</hibernate-configuration>
I thought that the mapping tag in the hibernate.cfg.xml should be enough to do the mapping of User
:
<mapping class="com.majidkhorsandi.dropbookmarks.core.User"/>
but Apparently, it was not. So What I had to do was to add the following extra line to the class in which I am building the SessionFactory
using the hibernate configuration (in my case HibernateUtil
):
configuration.addAnnotatedClass(User.class);
So eventually here is how my HibernateUtil
looked like after the fix:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
Configuration configuration = new Configuration().configure();
configuration.addAnnotatedClass(User.class);
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
static SessionFactory getSessionFactory() {
return sessionFactory;
}
}