Ok, I finally succeeded by adding the following property (so that the
session is tied to the current thread simply):
configuration.getDataSourceFactory().getProperties().put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS,
ThreadLocalSessionContext.class.getName());
(I added it in the "run(Bootstrap<C> bootstrap, Namespace namespace, C
configuration)" method before calling super.run that will initialize
hibernate).
And then I create a transaction with:
Transaction trans =
hibernate.getSessionFactory().getCurrentSession().beginTransaction();
to wrap around my code.
If there is something simpler, I'm interested :)
Le mardi 8 novembre 2016 10:06:01 UTC+1, [email protected] a écrit :
>
> Now I have new problems :)
>
> I get the following error:
> Exception in thread "main" org.hibernate.HibernateException: No session
> currently bound to execution context
>
> Should I use the UnitOfWorkAwareProxyFactory or something like that to
> make it work?
>
> Thanks!
>
> Le mardi 8 novembre 2016 09:43:22 UTC+1, [email protected] a écrit :
>>
>> Ha, so I simply need to implement an EnvironmentCommand :)
>>
>> Thanks for your help! I will see how it works!
>>
>> Le mardi 8 novembre 2016 09:36:18 UTC+1, [email protected] a écrit :
>>>
>>> It doesn't seem to work: getSessionFactory() returns null.
>>>
>>> I am using a ConfiguredCommand, maybe that's why? It seems to me that it
>>> is in the EnvironmentCommand that bootstrap.run is called in order to
>>> initialise bundles… (since it needs the environment to do so).
>>>
>>> Le mardi 8 novembre 2016 04:24:11 UTC+1, Evan Meagher a écrit :
>>>>
>>>> Hello Victor,
>>>>
>>>> If your new command's constructor takes an instance of
>>>> `HibernateBundle`, then you can use the value of
>>>> `hibernate.getSessionFactory()` within the command's run method to create
>>>> your DAO(s). All of the application's bundles are initialized and run
>>>> prior
>>>> to the invocation of the command's `run` method, so the database
>>>> connection
>>>> should be set up for you.
>>>>
>>>> On Mon, Nov 7, 2016 at 1:52 AM, <[email protected]> wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I would like to define some commands that exploit the DAO I defined
>>>>> with hibernate.
>>>>>
>>>>> I can't find a way to easily uses them: I would need to register the
>>>>> hibernate bundle in order to configure it with respect to my database
>>>>> configuration, but in a command, by definition, there is no bundles
>>>>> available!
>>>>>
>>>>> Any idea on how to do that elegantly?
>>>>>
>>>>> Thanks!
>>>>>
>>>>> --
>>>>> 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].
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Evan Meagher
>>>>
>>>
--
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].
For more options, visit https://groups.google.com/d/optout.