Hivemind creates proxy Hibernate Session

View: New views
4 Messages — Rating Filter:   Alert me  

Hivemind creates proxy Hibernate Session

by Alan Chaney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

I've based my Hivemind/Hibernate code on James Carman's excellent
example. I am using this in a new project in preference to the Spring
Hibernate framework because its simpler and easier to understand IMHO.
However, I've hit one slight snag. In part of my code I use a Hibernate
DetachedCriteria object which gets associated with a session by a
Hibernate call such as
DetachedCriteria dc = DetachedCriteria.forClass(MyClass.class);

(and then, much later inside the DAO)

Criteria cc = dc.getExecutableCriteria(Session session);

where session has been injected by Hivemind. However, it seems that
Hivemind injects a proxy and getExecutableCriteria casts to a
SessionImplementor and cannot cast the Hivemind created proxy giving me:

java.lang.ClassCastException: $Session_111be7b3773
    at
org.hibernate.criterion.DetachedCriteria.getExecutableCriteria(DetachedCriteria.java:51)
    ...

Is there any way to obtain the 'real' Session object from the proxy? Or
can anyone suggest another way to solve this problem. I looked at the
Spring equivalent which uses a special method within the Spring
HibernateTemplate class but I got lost trying to understand how the
HibernateCallback overcame this problem.

Thanks

Alan Chaney


Re: Hivemind creates proxy Hibernate Session

by Knut Wannheden :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Alan,

That indeed seems like a tricky problem. You cannot use the primitive
service model (which would have solved the problem because there would
be no wrapping proxy) as Hibernate sessions are not thread safe, nor
can you declare the implementation class SessionImpl as the HiveMind
service's interface as that class is final.

I think you should either declare the Hibernate SessionFactory as a
HiveMind service and inject that, or you could also define an
interface in your application which inherits from Session and
SessionImplementation and then use that to define your HiveMind
service. James, what do you think?

HTH,

--knut

On 4/4/07, Alan Chaney <alan@...> wrote:

> Hi
>
> I've based my Hivemind/Hibernate code on James Carman's excellent
> example. I am using this in a new project in preference to the Spring
> Hibernate framework because its simpler and easier to understand IMHO.
> However, I've hit one slight snag. In part of my code I use a Hibernate
> DetachedCriteria object which gets associated with a session by a
> Hibernate call such as
> DetachedCriteria dc = DetachedCriteria.forClass(MyClass.class);
>
> (and then, much later inside the DAO)
>
> Criteria cc = dc.getExecutableCriteria(Session session);
>
> where session has been injected by Hivemind. However, it seems that
> Hivemind injects a proxy and getExecutableCriteria casts to a
> SessionImplementor and cannot cast the Hivemind created proxy giving me:
>
> java.lang.ClassCastException: $Session_111be7b3773
>     at
> org.hibernate.criterion.DetachedCriteria.getExecutableCriteria(DetachedCriteria.java:51)
>     ...
>
> Is there any way to obtain the 'real' Session object from the proxy? Or
> can anyone suggest another way to solve this problem. I looked at the
> Spring equivalent which uses a special method within the Spring
> HibernateTemplate class but I got lost trying to understand how the
> HibernateCallback overcame this problem.
>
> Thanks
>
> Alan Chaney
>
>

RE: Hivemind creates proxy Hibernate Session

by Marcus.Schulte :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If you have your own HibernateSessionFactory-Impl, you create the
"core-Proxy" yourself, which has access to an unproxied hibernate
session.

So, I'd write an interface that extends org.hibernate.Session and adds
the method
Criteria reattachCriteria( DetachedCriteria dc );
which is implemented in your core-service-implementation-proxy and
returns "dc.getExecutableCriteria(session);", where session is the real
hibernate session, because you're inside the core service-impl.

Marcus

> -----Original Message-----
> From: Alan Chaney [mailto:alan@...]
> Sent: Wednesday, April 04, 2007 11:27 PM
> To: user@...
> Subject: Hivemind creates proxy Hibernate Session
>
> Hi
>
> I've based my Hivemind/Hibernate code on James Carman's
> excellent example. I am using this in a new project in
> preference to the Spring Hibernate framework because its
> simpler and easier to understand IMHO.
> However, I've hit one slight snag. In part of my code I use a
> Hibernate DetachedCriteria object which gets associated with
> a session by a Hibernate call such as DetachedCriteria dc =
> DetachedCriteria.forClass(MyClass.class);
>
> (and then, much later inside the DAO)
>
> Criteria cc = dc.getExecutableCriteria(Session session);
>
> where session has been injected by Hivemind. However, it
> seems that Hivemind injects a proxy and getExecutableCriteria
> casts to a SessionImplementor and cannot cast the Hivemind
> created proxy giving me:
>
> java.lang.ClassCastException: $Session_111be7b3773
>     at
> org.hibernate.criterion.DetachedCriteria.getExecutableCriteria
> (DetachedCriteria.java:51)
>     ...
>
> Is there any way to obtain the 'real' Session object from the
> proxy? Or can anyone suggest another way to solve this
> problem. I looked at the Spring equivalent which uses a
> special method within the Spring HibernateTemplate class but
> I got lost trying to understand how the HibernateCallback
> overcame this problem.
>
> Thanks
>
> Alan Chaney
>
>

Re: Hivemind creates proxy Hibernate Session

by Alan Chaney :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks, Marcus and Knut

Your suggestions are very useful. I did also find that another option -
it is possible to get the SessionFactory from the session itself and then
create a 'real' session from that - in fact I came across this in Java
Persistence With Hibernate [Bauer, King].

I've spent the day discovering that for various odd and irritating
reasons I may move away from the Crieteria approach anyhow.

Thanks for you help

Regards

Alan Chaney



Marcus.Schulte@... wrote:

> If you have your own HibernateSessionFactory-Impl, you create the
> "core-Proxy" yourself, which has access to an unproxied hibernate
> session.
>
> So, I'd write an interface that extends org.hibernate.Session and adds
> the method
> Criteria reattachCriteria( DetachedCriteria dc );
> which is implemented in your core-service-implementation-proxy and
> returns "dc.getExecutableCriteria(session);", where session is the real
> hibernate session, because you're inside the core service-impl.
>
> Marcus
>
>  
>> -----Original Message-----
>> From: Alan Chaney [mailto:alan@...]
>> Sent: Wednesday, April 04, 2007 11:27 PM
>> To: user@...
>> Subject: Hivemind creates proxy Hibernate Session
>>
>> Hi
>>
>> I've based my Hivemind/Hibernate code on James Carman's
>> excellent example. I am using this in a new project in
>> preference to the Spring Hibernate framework because its
>> simpler and easier to understand IMHO.
>> However, I've hit one slight snag. In part of my code I use a
>> Hibernate DetachedCriteria object which gets associated with
>> a session by a Hibernate call such as DetachedCriteria dc =
>> DetachedCriteria.forClass(MyClass.class);
>>
>> (and then, much later inside the DAO)
>>
>> Criteria cc = dc.getExecutableCriteria(Session session);
>>
>> where session has been injected by Hivemind. However, it
>> seems that Hivemind injects a proxy and getExecutableCriteria
>> casts to a SessionImplementor and cannot cast the Hivemind
>> created proxy giving me:
>>
>> java.lang.ClassCastException: $Session_111be7b3773
>>     at
>> org.hibernate.criterion.DetachedCriteria.getExecutableCriteria
>> (DetachedCriteria.java:51)
>>     ...
>>
>> Is there any way to obtain the 'real' Session object from the
>> proxy? Or can anyone suggest another way to solve this
>> problem. I looked at the Spring equivalent which uses a
>> special method within the Spring HibernateTemplate class but
>> I got lost trying to understand how the HibernateCallback
>> overcame this problem.
>>
>> Thanks
>>
>> Alan Chaney
>>
>>
>>