Two Laptop Bag*
The moose likes Object Relational Mapping and the fly likes Hibernate Transaction Demarcation in a non Web application. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Transaction Demarcation in a non Web application." Watch "Hibernate Transaction Demarcation in a non Web application." New topic
Author

Hibernate Transaction Demarcation in a non Web application.

Ainar Martin
Greenhorn

Joined: Mar 22, 2011
Posts: 11
Hello, I am new to Hibernate and I have some questions about transaction demarcations (I have already searched in Stack Overflow, the Hibernate Documentation.., but I haven't found the answer).

- First I generate the Hibernate Beans and DAOS with the Maven Hibernate Tools Plugin.

- After the generation process, I include my database access jar to my Java project.

Simple queries work fine, but when I execute complex queries with multiple joins, collections are not fetched (lazy fetching) and when I try to call the corresponding

get method to retrieve data, I get an lazy Initialization Exception since my session context is thread and the session has been closed with the transaction commit.

I need the session to live longer in order to fetch collections.

My hibernate cfg is:


<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydatabase</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
</session-factory>
</hibernate-configuration>

In this case is it necessary to use JTA transactions (I don't use a Web Server) or is it possible to achieve longer sessions not attached to the transactions scope (JDBC)?


Thank you.





lavnish lalchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 79
probably hibernate's famous session per view pattern can help you
https://community.jboss.org/wiki/OpenSessionInView?_sscc=t

lavnish.blogspot.com
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
Unfortunately, if you are in a non-web context, you can't use the OpenSessionInView filter or interceptor. As the document listed above points out, you either have to use detached objects, or you have to implement the equivalent of the OpenSessionInView filter yourself.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
One more note - this is not about transaction demarcation, this problem is about hibernate sessions.
Ainar Martin
Greenhorn

Joined: Mar 22, 2011
Posts: 11
Many thanks.

I have modified the hibernate tools code templates (ftl files) to generate custom DAOs and Beans.

With the Maven Hibernate Tools Plugin, I generate my database access layer.

Would it be reasonable to add to the Hibenate Beans, a method to reattach a detached object to a new Hibernate session? something like:

s.getCurrentSession().beginTransaction();
r.lock(p, LockMode.NONE);

once data would be retrieved by the getter method the transaction would be commited and the session would be closed. With this strategy I think the lazy initialization problem would be avoided.

Though I'm not sure if there would be any problems in multithread or multiprocess cases.


Thank you.
Ainar Martin
Greenhorn

Joined: Mar 22, 2011
Posts: 11
Is it possible to configure an Hibernate interceptor that executes before a getter method from an Hibernate Bean is called?

If this is possible, could the Hibernate Bean be attached to a session and detached after data is retrieved from database?

Thank you.
Ainar Martin
Greenhorn

Joined: Mar 22, 2011
Posts: 11
Hello where could I find a complete Hibernate 4 event listener example?

Is it possible to achieve on demand fetch of collections with listeners?

What is the best approach to fetch collections in a non web application with jdbc transactions?


Thank you.
 
jQuery in Action, 2nd edition
 
subject: Hibernate Transaction Demarcation in a non Web application.