1

Apologies for asking this when other people have asked before, but the other solutions appear to use @ComponentScan which is for Spring, and I am using dropwizard, so that wont work for me.

This is my (pretty simple) DTO class.

package apidto.entity.organization;

import lombok.Data;

import javax.persistence.*;

@Entity
@Table(name = "organization")
@NamedQueries({@NamedQuery( name = 
        "apidto.entity.organization.Organization.findAll",  
        query = "SELECT o from Organization o")})

public @Data class Organization {

    @Id
    private long id;

    @Column(name = "name")
    private String name;
}

and I have the following DAO class...

import apidto.entity.organization.Organization;
import io.dropwizard.hibernate.AbstractDAO;
import org.hibernate.SessionFactory;

import java.util.List;

public class OrganizationDAO extends AbstractDAO<Organization> {

    public OrganizationDAO(SessionFactory sessionFactory)
    {
        super(sessionFactory);
    }

    public List<Organization> findAll()
    {
        return list(namedQuery("apidto.entity.organization.Organization.findAll"));
    }
}
MickeyThreeSheds
  • 986
  • 4
  • 23
  • 42

2 Answers2

0

You should use ScanningHibernateBundle in the Application class. ScanningHibernateBundle will scan apidto.entitypackage and all nested packages and add all classes with @Entity annotation to the Hibernate SessionFactory.

class SomeApp extends Application<SomeConfiguration> {

    @Override
    public void initialize(Bootstrap<SomeConfiguration> bootstrap) {

        HibernateBundle<SomeConfiguration> hibernate = new ScanningHibernateBundle<SomeConfiguration>(
                "apidto.entity") {
            @Override
            public PooledDataSourceFactory getDataSourceFactory(SomeConfiguration configuration) {
                return configuration.getDataSourceFactory();
            }
        };

        bootstrap.addBundle(hibernate);
    }

}
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
0

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;
    }
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
MajiK
  • 637
  • 7
  • 10