Hibernate optional dependencies - java

I just downloaded Hibernate 4.2.3 Final and see that it has several optional libraries, though I'm not sure what it uses them for or under what circumstances they are needed/desired:
jboss-logging-3.1.0.GA.jar - is this a native SLF4J binding? Is there a way to have Hibernate not use this for logging, and instead use SLF4J and a different binding? If so, how?
C3P0 and Proxool JARs are also optionally included; are these the only two connection pool frameworks that Hibernate can be configured to use? What if I wanted to use, day, BoneCP? What if I wanted to let JNDI (Tomcat/DBCP) decide what connection pool to use?
What is hibernate-entitymanager?
What is hibernate-envers?

Hibernate now uses jboss-logging, refer to: How do you configure logging in Hibernate 4 to use SLF4J
I never heard of others, I'm sure they are good for most of use cases. If you want to use Tomcat/DBCP you can use as a JTA datasource. I don't think there is a connection provider for hibernate 3 or 4. Source: http://wiki.apache.org/commons/DBCP/Hibernate
If you want to use the HibernateEntityManager instead the javax.persistence.EntityManager, you can have the jar on your classpath and code with it.
Envers is an "automagical" auditing/versioning extension, where you annotate the Entities with #Audited and during the transaction, the changes would be also persisted. There is more here http://www.jboss.org/envers.

Related

Share Hibernate connection with other libraries

I'm writing a web application in Java and use Hibernate to map the database with my java objects. In Hibernate I connect to the database using C3PO pool. Now I need to schedule some tasks. Herefore I will use Quartz. Now the scheduled tasks will be stored in the database. It is possible to share the connection pool of C3PO with Quartz? So that the database settings are in one and the same file and that only one library is responsible to open a databaseconnection.
This is sort of a subjective thing, but suffice it to say, if you want to share the connection you have a number of options.
If you are using spring, define the c3p0 connection pool as a bean and inject it into both the Hibernate session factory and the Quartz scheduler beans.
If you are trying to stay "pure" and not using Spring, you can define your a JNDI based data-source at the container level.
There are some nuances with both approaches.
Unfortunately without indicating how "portable" you are trying to be and what you are currently using beyond just 'java', 'quartz-scheduler' and 'c3p0' as tags an answer will be vague at best.
EDIT
Thanks to the OP for adding additional information.
So with regard to that information, if you are using a Dynamic Web Project, you may be able to add a container specific deployment descriptor to WebContent directory.
For tomcat, META-INF/context.xml
For Jetty, WEB-INF/jetty-web.xml
With those, you can define a JNDI data source. Refer to the specific container for information on how to do that. Once done, Hibernate and Quartz can be configured to use a JNDI reference for the data source you have configured.

Spring Roo - Bypass Hibernate

I have a Spring/Roo application which uses PostgreSQL and Hibernate.
As is appropriate, the connection information is located in the database properties file
src/main/resources/META-INF/spring/database.properties
Unfortunately, I have a situation where querying the database through Hibernate is draining the resources too much. I am sure that I can extract the database information (url/username/password) from the file listed above, but I am not sure where to begin my search.
Is there a manual or otherwise where I can find this information?
If you wish to bypass Hibernate to write more efficient queries by hand, you don't have to make separate connections to do it, and should not do so.
Get a Hibernate session and unwrap it to get the underlying java.sql.Connection. Or use native SQL via Hibernate's own interfaces.
That way you still get to use the useful bits of Hibernate, like the connection pooling integration. Sharing the same connection pool as Hibernate will improve efficiency, and you'll have a lot less extra code if you do it this way.
I haven't used Spring Roo, so I can't speak specifically for it. Here's info for Hibernate used via JPA or here. For direct Hibernate usage where you have a Session object, use Session.connection() on old versions of Hibernate, or the Work interface on newer versions:
Session.doWork()
session.connection() deprecated on Hibernate?
How to get jdbc connection from hibernate session?
Alternative of deprecated hibernate getSession().connection()
If you insist on doing this by hand anyway, start with ClassLoader.getResource(...).

Trouble using Guice Persist together with JTA / bitronix

We have an existing j2se project that already uses JPA and guice-persist. Now, because we want to add JMS functionality, there is a request for 2-phase-commit and JTA. We'll use the bitronix transaction manager because there's no container (like spring).
To my understanding, the first thing we have to do is to change the transaction-type of the persistence unit from RESSOURCE-LOCAL to JTA, because we want database transactions to vote for commit rather then commit. The commit is done on phase 2 after collecting all votes.
With guice-persist we use the #Transactional annotation for methods that should run in a single transaction. The JPAPersistModule provides an EnitiyManagerFactory and it is used for guice-persist internal classes, like JpaLocalTxnInterceptor that wraps the annotated methods.
Now I get exceptions like
java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
at org.hibernate.ejb.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:1009)
at com.google.inject.persist.jpa.JpaLocalTxnInterceptor.invoke(JpaLocalTxnInterceptor.java:57)
...
because the JpaLocalTxnInterceptor calls getTransaction() on the provided entity manager.
I'm quite stuck, at the moment. Is there any way to use guice-persist together with JTA or o we really have to drop guice-persist from the project? Or, is there any replacement for guice-persist if we want to do JTA (with Bitronix)?
Had a similar situation. In our case we were using Guice + Jooq. We wanted Jooq because we have a large legacy Rails DB and wanted fine control plus speed. We picked Guice over Spring because we felt it is a better framework, and it is much faster and we like compile time checking.
We could NOT use Guice persist with Jooq, so we :
Use Atomikos JTA (free version)
Wrote our own #Transactional AOP annotation interceptor;
Our injectable Service provides the java.sql.Connection to our jooq processors, but always supply an Atomikos DataSource bean
We basically modified this code:
http://www.dailyjavatips.com/2011/10/24/database-transactions-google-guice-aop/
So that example uses regular JDBC Tx, but we modified it so it would use Atmomikos' JTA aware Tx instead.
Works like a charm!
Oje

Annotations in domain objects in client pull complete hibernate dependencies

I want to share some domain objects between my client and server apps.
Starting with Hibernate 4, the Hibernate annotations were integrated in hibernate-core (used to be a separate jar), see http://in.relation.to/Bloggers/NoMoreHibernateannotationsModule
I would really prefer to stick with annotations (and not switch to xml based mapping).
At the same time I would really like to avoid bundling the hibernate core dependencies with the client (which will be transferred to the user via http through java web start, ideally on every startup).
Do I have to go back to Hibernate 3.x? Are these annotation dependencies needed to run the client or do I just need them to compile the client?
Thanks
You don't need to have the annotations of a class in the classpath to be able to load a class.
That said, a Hibernate entity contains collections and references to other entities. The collections are instances of Hibernate collections, and the references can be Hibernate proxies, if they're lazy-loaded. So if you serialize a Hibernate entity from the server to the client, the client will need the Hibernate jar(s) in its classpath to be able to load them.
If you want to avoid the Hibernate dependency at client-side, you should consider transferring DTOs to the client rather than Hibernate entities.

Best Tomcat6 JNDI + Hibernate configuration for session/transaction support

Quote from hib official docs:
Starting with version 3.0.1, Hibernate added the SessionFactory.getCurrentSession() method. Initially, this assumed usage of JTA transactions, where the JTA transaction defined both the scope and context of a current session. Given the maturity of the numerous stand-alone JTA TransactionManager implementations, most, if not all, applications should be using JTA transaction management, whether or not they are deployed into a J2EE container. Based on that, the JTA-based contextual sessions are all you need to use.
end of quotation
I use tomcat6 as a servlet container, and need to integrate Hibernate 3.2 into the project.
I found a document how to configure SessionFactory lookup through JNDI, though it does not work as expected.
Quote:
Hibernate works in any environment that uses JTA, in fact, we recommend to use JTA whenever possible as it is the standard Java transaction interface.
End of quote.
In hibernate config I indicate current_session_context_class = jta
and now I get an error "No TransactionManagerLookup specified".
The problem is that Tomcat does not support JTA, and to get it worked,
if I understand it correctly,
you need to add JOTM or other library to Tomcat.
But according to quote it is recommended to use JTA.
What can you recommend in this situation?
Your choices are pretty clear:
Either change to a servlet container that does support JTA
Find another transaction mechanism other than JTA, such as programatically controlling transactions yourself.
What can you recommend in this situation?
Do without JTA and use the Open Session In View pattern to handle the session.

Categories