Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Transaction Demarcation in a non Web application.

 
Ainar Martin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably hibernate's famous session per view pattern can help you
https://community.jboss.org/wiki/OpenSessionInView?_sscc=t
 
Emanuel Kadziela
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more note - this is not about transaction demarcation, this problem is about hibernate sessions.
 
Ainar Martin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic