Hi all,

I'm having some difficulties making Hibernate work in DropWizard. I'm new
to both Hibernate and DW, so I apologize if my questions are stupid.

I set up my tests like so:

@ExtendWith(DropwizardExtensionsSupport.class)
public class UpdateTest {
    private SessionFactory sessionFactory;

    public DAOTestExtension database = DAOTestExtension.newBuilder()
        .addEntityClass(Foo.class)
        .build();

    @BeforeEach
    public void setup() {
        sessionFactory = database.getSessionFactory();
    }

There are a few things I'm not understanding.

1) I can't use this SessionFactory as I normally would. The first of these
createQuery calls succeeds, and the second fails (with "Calling method
'createQuery' is not valid without an active transaction"):

   try (Session session = sessionFactory.openSession()) {
       Transaction transaction = session.beginTransaction();
       session.createQuery(...).executeUpdate();
       sessionFactory.getCurrentSession().createQuery(...).executeUpdate();
       transaction.commit();
   }

I understand that I'm *supposed* to use database.inTransaction(), but I
don't understand why the above fails. Maybe this won't matter so much once
I get used to it.

2) I can't figure out whether it's possible to set up config. For example,
can I configure the driver, jdbc URL, dialect, etc? Or is there no way to
connect to a real db? Maybe I'm mixing concerns here, but I'd like my DAO
tests to also test things like db constraints.

Normally this can be done with (org.hibernate.cfg.)Configuration or
(io.dropwizard.db.)DataSourceFactory, but I don't think I can do that here.
Or it can be done through a hibernate.cfg.xml or hibernate.properties, but
Hibernate doesn't find hibernate.cfg.xml, and loads but doesn't use
settings from hibernate.properties.

Thanks,
Aditya

-- 
You received this message because you are subscribed to the Google Groups 
"dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/dropwizard-user/CAMx7i44sGwfNMnwpXq8e4pCcXBuiExZwgMRM%2B0qOFVrTJ-d5MQ%40mail.gmail.com.

Reply via email to