Issue Details (XML | Word | Printable)

Key: SPR-300
Type: New Feature New Feature
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Juergen Hoeller
Reporter: Artur Karazniewicz
Votes: 14
Watchers: 10
Operations

If you were logged in you would be able to see more operations.
Spring Framework

Support for hibernate3 in sandbox

Created: 31/Aug/04 08:06 AM   Updated: 26/Mar/05 03:25 PM
Component/s: None
Affects Version/s: None
Fix Version/s: 1.2 RC1

Time Tracking:
Not Specified

File Attachments: 1. File hibernate-patch.tgz (38 kB)
2. Zip Archive hibernate3-annotations.zip (92 kB)
3. Zip Archive hibernate3Spring.zip (60 kB)
4. Java Source File LocalAnnotationSessionFactoryBean.java (3 kB)
5. GZip Archive spring-hibernate3-1.tar.gz (50 kB)
6. File spring-hibernate3-final.tgz (52 kB)
7. Zip Archive spring-hibernate3-upd.zip (137 kB)
8. GZip Archive spring-hibernate3.tar.gz (50 kB)
9. File spring-hibernate3.tgz (50 kB)



 Description  « Hide
Hello,

Last few days I worked on support for hibernate3 in spring sandbox. I created new packages in sandbox and refactored org.springframework.orm.hibernate (and support) packages into
org.springframework.orm.hibernate3 in sandbox. The task was quite easy, there are minor problems though:

* hibernate package changed to org.hibernate,
* hibernate team split Session into two pieces org.hibernate.Session and
org.hibernate.classic.Session (extends Session) - where some deprecated methods live. SessionFactory returns now "classic" session, but I decided to use org.hibernate.Session everywhere,
* hibernate team changed exceptions to unchecked and changed method signature for getPersistentClass() it returns String now (instead of Class),
* saveOrUpdateCopy(..) and int delete(...) dissapeard (in fact these methods are moved into "classic" session), so I removed them
from HibernateTemplate
* I ported complete test suite from spring hibernate packages, all tests passed (with minor changes, though)

Currently i didn't implemented new methods from Session yet, but will do it ASAP. This project uses this - two new jars must be added into project: hibernate3.jar and antlr-2.7.4.jar.

This project must be compiled against CVS HEAD, since it needs some changes done by Juergen recently.

Hope it'll help people interested in new hibernate support in spring.

Artur

 All   Comments   Work Log   Change History   FishEye   Builds      Sort Order: Ascending order - Click to sort in descending order
Artur Karazniewicz added a comment - 31/Aug/04 12:05 PM
Updated file

Artur Karazniewicz added a comment - 31/Aug/04 12:07 PM
Fixed file uploaded.

Artur Karazniewicz added a comment - 31/Aug/04 12:26 PM
Fixed directory names

Artur Karazniewicz added a comment - 02/Sep/04 05:33 AM
* complete (?) support for HB3-alpha in sandbox
* added new "entity-name" related methods
* added test suite for new methods

Keith Garry Boyce added a comment - 27/Sep/04 05:24 PM
Is there a place where we can download the nightly sandbox binary jar?

Artur Karazniewicz added a comment - 28/Sep/04 06:48 AM
Unless it is added into sandbox You have to complite this for yourself.

Artur

Juergen Hoeller added a comment - 28/Sep/04 06:54 AM
Note that Hibernate3 is still alpha. It's not feasible to ship support for alpha software in a Spring production release, therefore we can't include this in Spring proper until Hibernate3 becomes at least beta (which is likely to happen within the Spring 1.2 timeframe).

Juergen

Erwin van der Koogh added a comment - 20/Oct/04 07:46 AM
At 20 October 2004 I have tried to do exactly this:

This is all done within Eclipse, just in case it might matter.

- Update spring from CVS.
- Add hibernate3.jar and antlr-2.7.4 to the lib directory.
- Add those jars to the buildpath.
- Download your spring-hibernate3-final.tgz (52 kb) from this issue.
- Copy the src and test directory to the sandbox directory within the spring project.
- clean the project and do a complete rebuild.

The problem is that I am getting a compile error on org.springframework.orm.hibernate3.HibernateTransactionObject:

"Class must implement the inherited abstract method SmartTransactionObject.isRollbackOnly()"

The weird thing is that I can not find any changes anywhere that this would have worked anywhere. Your spring-hibernate3.1 version doesn't include this method either and browsing through the CVS history I find nothing relevant is changed that could have any impact on this.

Am I missing something obvious?

It's easy enough to add this method, but I don't really know what this is supposed to be doing and what it should be doing and want to make sure it's done right.

Thanks for the patch!

Artur Karazniewicz added a comment - 20/Oct/04 01:33 PM
Just cut and paste unimplemented method from spring's HibernateTemplate.

Artur

n alphonse added a comment - 26/Dec/04 10:28 PM
The problem is that I am getting a compile error on org.springframework.orm.hibernate3.HibernateTransactionObject:

"Class must implement the inherited abstract method SmartTransactionObject.isRollbackOnly()"
jou say "Just cut and paste unimplemented method from spring's HibernateTemplate."

please tell me where ant what unimplemented method i cut and paste ?

thank

Behrang Javaherian added a comment - 13/Jan/05 02:15 AM
The patch seems need some changes to be compilable. I changed the patch now it seems compilable. Add fixed the isRollbackOnly problem and provides dummy implementation for hashCode and replace method in AbstractLobType class.

Anthony Johnson added a comment - 20/Jan/05 10:32 AM

I have modified the code to work with the latest Hiberate3 code.
It compiles fine, and all Unit tests pass.

Joe Shevland added a comment - 26/Jan/05 05:46 PM
Thanks for the code for the latest Hibernate 3, I had a few of the minor issues mentioned (having to implement hashCode(), replace() - I used the -final bundle) and I also modified LocalSessionFactoryBean to use the Hibernate Annotations 3.0 alpha 2 package (along with supporting annotatedClasses and annotatedPackages). The code changes were pretty minor but happy to share if anyone wants them.

Joe Shevland added a comment - 08/Feb/05 03:34 PM
Uses the -final package provided and alters is slightly to use the new Hibernate 3 AnnotationConfiguration method.

Joe Shevland added a comment - 08/Feb/05 03:38 PM
The -annotations one I've uploaded will have rough edges, and no work was put into refactoring the base packages, cleaning up comments etc. Caveat empor but let me know if you have any troubles... ideally a new hibernate3 Spring package in CVS would help things along so ppl can make a start.

Stefano Bagnara added a comment - 08/Feb/05 05:29 PM
I'm trying to compile this against hibernate3 beta3 but it doesn't work.

1) The method afterTransactionCompletion(boolean, Transaction) in the type SessionImplementor is not applicable for the arguments (boolean) SessionFactoryUtils.java (src/org/springframework/orm/hibernate3 line 701)

It seems that the signature of that method changed just after hibernate 3b2: now it needs a second argument including the Transaction.

2) LocalTransactionManagerLookup import a net.sf.hibernate class instead of the org.hibernate one!

3) AbstractLobType does "import org.springframework.orm.hibernate.LocalSessionFactoryBean" instead of
"import org.springframework.orm.hibernate3.LocalSessionFactoryBean" (notice the "3" in the hibernate package name)

Noddy Casper added a comment - 12/Feb/05 06:34 PM
Can you guys please fix the problem so we can integrate Spring with the latest Hibernate3 version? I am just wondering that Hibernate3 has already been delivered beta4, Spring is supposed to support it currently.

Noddy

Ben added a comment - 16/Feb/05 01:39 PM
Hi,

Can anyone give an update to the status of support for the Hibernate 3 Beta 4 release?

Thanks

Stefano Bagnara added a comment - 16/Feb/05 01:45 PM
I think that hibernate b4 is the same as beta 3 from spring views.
The latest code attached does not compile due to the few code I've already commented in a previous comment.

I know that hibernate 3 allow more powerful operations that hibernate2. One for all: if I remember correctly in H3 you can avoid the session.close() call. I think many H3 improvements could bring a cleaner integration with spring but I also think that this integration should be done by someone that knows well h3 and spring internals.

Juergen Hoeller added a comment - 16/Feb/05 01:51 PM
My current plan is to do a fresh port of the most recent Hibernate 2.1 support in "orm.hibernate" to the "orm.hibernate3" package, as soon as Spring 1.1.5 has been released (currently scheduled for Feb 28).

Hibernate3 support will be officially released soon after, in Spring 1.2 RC1 (currently scheduled for mid March). It will already be available right after the 1.1.5 release in nightly 1.2-RC1-dev snapshots.

This should coincide nicely with the first Hibernate3 release candidate (with the Hibernate3 API frozen). If there is an urgent need for earlier Hibernate3 support, I would suggest uploading a preliminary add-on jar file to JIRA.

Juergen

Juergen Hoeller added a comment - 16/Feb/05 02:54 PM
Well, it's not that you can't avoid the Session.close call: As far as I understand, all you can get is automatic closing at *JTA* transaction completion - for *each* Session you open during a JTA transaction.

So even with Hibernate3, Spring's resource management adds a lot of value: using the *same* Session during a transaction, even for multiple operations (multiple getSession calls) - both for JTA transactions and local transactions.

Consequently, I expect Spring's support for Hibernate3 to be very analogous to the current support for Hibernate 2.1, both in terms of resource management and in terms of HibernateTemplate operations.

I agree that Spring's Hibernate3 support should work with the native Hibernate3 API, not the classic API that they keep for backwards compatibility - even if this means dropping a couple of operations on the Hibernate 2.1 HibernateTemplate.

Juergen

Jonas Van Poucke added a comment - 17/Feb/05 10:28 AM
For HibernateB4, in SessionFactoryUtils, you should use

((SessionImplementor) session).afterTransactionCompletion(
status == STATUS_COMMITTED,
sessionHolder.getTransaction());

in the method public void afterCompletion(int status)

I guess..

Jonas

Henri Dupre added a comment - 24/Feb/05 09:47 AM
The hibernate operations should be changed for hibernate 3. I personnaly don't like so much all the "find" operations... I'd rather like to have a similar interface than the hibernate session with createQuery and createCriteria.

Henri Dupre added a comment - 24/Feb/05 10:02 AM
the merge operations should be added to HibernateOperations

Henri Dupre added a comment - 28/Feb/05 05:03 PM
There is an updated version of the hibernate 3 bindings called spring-hibernate3-upd.zip. I added a setting to enable the JMX management which allows you to see hibernate 3 statistics.
<property name="JMXEnabled">
<value>true</value>
</property>
Is required in the applicationContext.xml in the hibernate sessionFactory bean to enable statistics on hibernate and also register the hibernate server into the JVM built-in JMX server.

I also added the merge operation. This compiles and works with the RC1.

matthew inger added a comment - 03/Mar/05 05:57 PM
I have attached a file LocalAnnotationSessionFactoryBean.
It basically completes the integration of Hibernate3, Spring, and
Hibernate3 annotations. It is an extension of the standard spring
LocalSessionFactoryBean, which creates an AnnotationConfiguration instead of a standard Configuration, and has a new property "annotatedClasses". The list of classes will be added to the configuration via the "addAnnotatedClass" as part of the overridden postProcessConfiguration method. You can now put the following in a spring config file, and be able to use annotated classes instead of hibernate mapping files.

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalAnnotationSessionFactoryBean">
    ...
    <property name="annotatedClasses">
        <list>
            <value>MyObjectImpl</value>
        </list>
    </property>
    ...
</bean>

matthew inger added a comment - 03/Mar/05 06:00 PM
FYI: In regards to my attachment of the LocalAnnotationHibernateSessionFactory class, i'm not expecting it to be distributed in the same jar with all the other files, but i think it should be in it's own optional jar along with any other JDK 5 features. I understand the need to keep backward compatibility, but it doesn't mean that the early adopters should suffer, and putting it in it's own jar will appease both groups.

Cameron Braid added a comment - 05/Mar/05 01:00 AM
are the patches associated with this issue going to be the code that gets put into the sandbox, or are there plans to do something different ?

Also, what is the timeframe for Hibernate 3 support - it has a release candidate now.

Juergen Hoeller added a comment - 05/Mar/05 04:13 PM
I've committed Hibernate3 support ("orm.hibernate3" package) to Spring CVS, as fresh port of the current Hibernate 2.1 support. This should be available in the nightly Spring 1.2-dev snapshot by Monday.

The first official release that includes Hibernate3 support will be Spring 1.2 RC1, due in about two weeks. This will also include JMX support and other new features.

Juergen

Artur Karazniewicz added a comment - 06/Mar/05 08:16 AM
Currently this is only copy of old "Session" interface. Please add new methods from org.hibernate.Session interface.

Juergen Hoeller added a comment - 06/Mar/05 12:45 PM
The Hibernate3 support as currently in CVS already features the new "persist" and "merge" operations, so should be pretty complete in terms of Hibernate3 operations.

We don't have operations for "entity names" yet, that is, Hibernate3's support for Maps etc. I don't intend to add such methods, actually: Spring focuses on rich domain objects, so persistent Maps as DAO layer arguments don't really fit in.

Of course, any Hibernate3 Session method can be called in a HibernateCallback, where the native Hibernate Session will be exposed. The full Hibernate Query and Criteria facilities are available there. The operations on HibernateTemplate are just conveniences.

Alternatively, you can also work with direct Session access (SessionFactoryUtils.getSession or HibernateDaoSupport.getSession), which is particularly straightforward if you can guarantee execution within a transaction (where no close call is necessary, because the Session will be managed by the transaction).

With Hibernate3, you could even code such direct Session access without special exception handling - provided that you're willing to let the now unchecked HibernateException through at your DAO layer. We still recommend throwing an implementation-independent exception like Spring's DataAccessException at the DAO layer, though.

Juergen

Henri Dupre added a comment - 06/Mar/05 01:11 PM
I just looked at the files in the repository... There should be some way to enable hibernate JMX support. I think LocalSessionFactoryBean is the proper place to do it (I added that code in my zip file). It is a important feature especially regarding spring support of JMX.

Juergen Hoeller added a comment - 10/Mar/05 08:29 AM
We don't need to build support for the JMX StatisticsService into LocalSessionFactoryBean itself. Spring's JMX support can be leveraged to expose the StatisticsService in a declarative way: just a define a corresponding MBeanExporter.

<bean class="org.springframework.jmx.export.MBeanExporter">
  <property name="beans">
    <map>
      <entry key="org.hibernate.jmx.StatisticsService:type=StatisticsService"><ref bean="statisticsService"/></entry>
    </map>
  </property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  ...
</bean>

<bean id="statisticsService" class="org.hibernate.jmx.StatisticsService">
  <property name="sessionFactory"><ref bean="SessionFactory"/></property>
</bean>

Alternatively, rely on MBeanExporter's autodetection of MBeans and implicit naming through the bean name:

<bean class="org.springframework.jmx.export.MBeanExporter"/>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  ...
</bean>

<bean name="org.hibernate.jmx.StatisticsService:type=StatisticsService" class="org.hibernate.jmx.StatisticsService">
  <property name="sessionFactory"><ref bean="SessionFactory"/></property>
</bean>

Juergen

Juergen Hoeller added a comment - 12/Mar/05 11:21 AM
Completed for 1.2 RC1. Full Hibernate3 support as expected; support for Hibernate's JMX statistics service through Spring's new JMX support rather than hard-wired.

No direct Hibernate3 annotations support yet: we want to avoid building the main Spring download on JDK 1.5 for the time being.

Hibernate's alpha annotation support can be activated through the new "configurationClass" property on LocalSessionFactoryBean, with the annotated packages and annotated classes getting specified in the standard "hibernate.cfg.xml" file:

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation">
    <value>classpath:hibernate.cfg.xml</value>
  </property>
  <property name="configurationClass">
    <value>org.hibernate.cfg.AnnotationConfiguration</value>
  </property>
</bean>

Alternatively, create a custom subclass of LocalSessionFactoryBean and register your annotated packages and classes in a custom "postProcessConfiguration" implementation, casting the Configuration object to AnnotationConfiguration.

Juergen

Artur Karazniewicz added a comment - 14/Mar/05 06:46 AM
I'm missing something or I cannot see "entityName"-related methods. This is very usefull feature and IMHO should be available via spring's HibernateTemplate.

David Otaguro added a comment - 14/Mar/05 07:57 PM
Is there any Spring support for Hibernate's new row-level security (session filters)? When I open a new session, I want to call session.enableFilter() with some transaction-specific security context information (like current user), but there doesn't appear to be a clean way to ensure that any call to the session factory's getSession() guarantees that the session returned has the appropriate filters set on it.

What I'm doing as a workaround is overriding createHibernateTemplate() in my HibernateDaoSupport subclass to wrap the session factory used by the template in a delegating SessionFactory that ensures the session filters are set for any calls to getSession(*). That keeps me from having to modify any of the Spring source.

Does that sound like a decent way to go? I was thinking the alternative would be to add some callback pattern in the SessionFactoryUtils (a la the interceptor) that's called at create session time, but I'm reluctant to modify the Spring source given my level of unfamiliarity with the guts of Spring.

ian green added a comment - 15/Mar/05 04:31 PM
Yeah, what happened to createQuery etc as in the old HibernateTemplate? trying to write a query and only return the first n results....

Juergen Hoeller added a comment - 16/Mar/05 04:08 AM
The createQuery/createCriteria methods on HibernateTemplate have often been misunderstood and misused: They were intended to be called *within HibernateCallback implementations*, to allow to prepare Query objects in special ways. This was also the reason why those methods threw HibernateException rather than DataAccessException.

This usage style was clearly outlined in the javadoc, but most people nevertheless called them in their DAOs directly, handling HibernateExceptions in custom ways etc. For this reason, those methods have been deprecated in Spring 1.1.x, and now removed in Spring 1.2.

The Hibernate Session exposed to a HibernateCallback implementation is now proxied and applies special preparing of created Query objects. Therefore, simply use Session.createQuery/createCriteria within a HibernateCallback instead - the native Hibernate API -, usually within an anonymous HibernateCallback class:

List result = getHibernateTemplate().executeFind(new HibernateCallback() {
  public Object doInHibernate(Session session) throws HibernateException {
    Query query = session.createQuery("some HQL string");
    ...
    return query.list();
  }
});

Juergen