Red Hat

In Relation To Hibernate ORM

In Relation To Hibernate ORM

Multitenancy and current session

Posted by Emmanuel Bernard    |    Jun 26, 2015    |    Tagged as Hibernate ORM

Today let’s discuss the interaction between multitenancy and the current session feature.

Multitenancy let’s you isolate Session operations between different tenants. This is useful to create a single application isolating different customers from one another.

The current session feature returns the same session for a given context, typically a (JTA) transaction. This facilitates the one session per view/transaction/conversation pattern and avoids the one session per operation anti-pattern.

Session session = sessionFactory.getCurrentSession();
// do some other work
[...]
// later in the same context (e.g. JTA Transaction)
Session session2 = sessionFactory.getCurrentSession();

// semantically we have
assert session == session2

The two features work well together, simply implement CurrentTenantIdentifierResolver. That will give Hibernate ORM the expected tenant id when the current session is created.

How current is current?

When discussing with Florian and the ToulouseJUG, we exchanged on a small case where things might not work as you expect. Hibernate ORM considers that, for a given context (e.g. transaction), there can only be a single current Session.

So if in the same context (e.g. transaction):

  • your CurrentTenantIdentifierResolver implementation returns different values of tenant id,

  • you use getCurrentSession()

you will get a TenantIdentifierMismatchException.

However, it is sometimes useful to be able to reach several tenants from the same context. You have two options:

  • Manually create the Session

  • Implement a custom CurrentSessionContext

Manually create the Session

You can use the SessionFactory API to create a Session for the tenant id you are looking for.

Session session1 = sessionFactory.withOptions()
        .tenantIdentifier( tenant1 )
        ...
        .openSession();
Session session2 = sessionFactory.withOptions()
        .tenantIdentifier( tenant2 )
        ...
        .openSession();

But you have to make sure to close these sessions. If you are used to CDI or Spring handling sessions for you, or if you rely on the current session feature to propagate the session across your stack, this might be annoying.

Implement a custom CurrentSessionContext

The alternative is to implement your own version of CurrentSessionContext, avoid raising the TenantIdentifierMismatchException, and keep Session instances per both context and tenant id.

In practice, current sessions are stored in a ConcurrentHashMap keyed by context identifier, you just need to improve that part. Start with JTASessionContext and hack away!

What now?

We are currently discussing whether the default CurrentSessionContext implementations should partition by tenant id or raise the exception. If you have your opinion, chime in!

In the mean time, use one of the options above.

When creating a bug report for any project within the Hibernate family, it’s extremely helpful (and, frankly, required) to have an adequate test case available. This is obviously important to make reproducing the issue as easy as possible. But it’s also vital longer-term. Nearly every bug fix should include a regression test, which is frequently based on the original reproducer (sometimes, it’s the reproducer, verbatim).

To help create useful test cases, we’re opening up a repo with various templates. Please see the READMEs in each project’s subdir for more info: Hibernate Test Case Templates

As a starting point, the repo contains two templates for ORM:

  • ORMUnitTestCase: By far, this one’s the most helpful. ORM includes a built-in unit test framework that does much of the heavy lifting for you. All that’s required is your entities, logic, and any necessary settings. Since we nearly always include a regression test with bug fixes, providing your reproducer using this method simplifies the process. We can then directly commit it, without having to mold it in first. What’s even better? Fork hibernate-orm itself, add your test case directly to a module’s unit tests (using the template class), then submit it as a PR!

  • ORMStandaloneTestCase: This template is standalone and will look familiar. It simply uses a run-of-the-mill ORM setup. Although it’s perfectly acceptable as a reproducer, lean towards ORMUnitTestCase whenever possible.

The eventual goal is to also include templates for Validator, Search, and OGM.

As always, this is open source for a reason! If the templates can be improved in any way, please let us know (either through our JIRA instance or through GitHub Issues). Better yet, send us a pull request!

For those of you using Hibernate ORM version 5.0.0.CR1, you can now use the freshly released Hibernate Search 5.4 version 5.4.0.Alpha1.

What's new

Absolutely nothing! This Hibernate Search version is identical in terms of features and API to version 5.3.0.CR1: this should make it easier for you all to upgrade the Hibernate ORM libraries (hibernate-core, hibernate-entitymanager,..) without the distraction of changes because of Hibernate Search: focus on the changes you'll need to apply because of the major version upgrade of Hibernate (if any, as it's not too complex at all).

WildFly compatibility and JBoss Modules

With every release of Hibernate Search we normally also release a set of modules to run the latest version of it on WildFly, but in this case since the updated Hibernate ORM 5 integrations for WildFly have yet to be released, we skipped this step. Fear not, the WildFly integration will be finished soon and we'll then resume releasing such module packs as usual. Not least, this very same version of Hibernate Search will soon be available in WildFly 10, so the modules missing today won't actually be needed at all.

This is a great time to try Hibernate 5

While the latest polish is performed on Hibernate 5, we're all looking forward for feedback from you. It is likely that some more changes will be done, but we consider it good enough already to not expect any regression so please try it and let us know! We're at that sweet spot in which you can still propose changes without the chains of strong API compatibility requirements, but good enough for you to not be wasting time on a quickly changing target.

Versions reminder

This version of Hibernate Search requires:

  • Hibernate ORM 5.0.0.CR1
  • Apache Lucene 4.10.x
  • Java SE 7

Our rules and conventions for versions and compatibility are documented here on the GitHub Wiki.

  • Artefact jars are available on Maven Central under the GAV org.hibernate:hibernate-search-orm:5.4.0.Alpha1
  • Zip and tar bundles are available via our website
  • Feedback is welcome on the forums and emails, IRC

Hibernate ORM 5.0.0.CR1 Release

Posted by Steve Ebersole    |    May 28, 2015    |    Tagged as Hibernate ORM Releases

The Hibernate team is proud to announce the first candidate release for 5.0. The main changes since the Beta2 release include:

  • Initial generic typing for Hibernate native (Session, e.g.) APIs
  • Continued development of the new bytecode enhancement capabilities including improved support in the Maven and Gradle plugins
  • Support for parameterized types with AttributeConverter implementations
  • Improved OSGi integration testing using Pax Exam and Karaf, and publishing a hibernate-osgi Karaf features file
  • Auto quoting of keywords used as identifiers (User, e.g.).
  • Incorporating fixes, improvements and suggestions to the improved schema update and validation tooling

A lot of help from the community went into this release and the previous Beta releases. Thanks for all the help and work and testing and doggedness!

As always, see http://hibernate.org/orm/downloads/ for information on obtaining the releases.

Report any issues to Hibernate Jira

Hibernate ORM 4.3.10.Final Released

Posted by Gail Badner    |    May 15, 2015    |    Tagged as Hibernate ORM Releases

The primary reason for this release is to add support for running Hibernate ORM with Infinispan 7.2.1.Final as a runtime dependency. Hibernate still uses Infinispan 6.0.0.Final by default.

The plan is for WildFly 9 to include Hibernate ORM 4.3.10.Final. Since WildFly 9 uses Infinispan 7.2.1.Final, we had to make some corrections so that Hibernate ORM works properly with Infinispan 7.2.1.Final (as well as 6.0.0.Final) as a cache provider. To avoid backward-compatibility issues, Hibernate still uses an Infinispan 6.0.0.Final configuration by default.

In order to use Hibernate ORM with Infinispan 7.2.1.Final, it is necessary to specify an Infinispan 7.2 configuration using the environment variable, hibernate.cache.infinispan.cfg. An example of an Infinispan 7.2 configuration has been added as a test resource here.

For more information about changes to support Infinispan 7.2.1.Final, see: HHH-9776 and HHH-9781.

Other changes were minor. For more information, please see: https://hibernate.atlassian.net/jira/secure/ReleaseNote.jspa?projectId=10031&version=19650

JBoss Nexus: https://repository.jboss.org/nexus/content/groups/public/org/hibernate
Maven Central: http://repo1.maven.org/maven2/org/hibernate/hibernate-core (should update in a couple of days)

SourceForge: https://sourceforge.net/projects/hibernate/files/hibernate4
4.3.10.Final Downloads: 4.3.10.Final ZIP, 4.3.10.Final TGZ

Hibernate ORM 5.0.0.Beta2 Release

Posted by Steve Ebersole    |    Apr 30, 2015    |    Tagged as Hibernate ORM Releases

I have just finished releasing Hibernate O/RM 5.0.0.Beta2. Beyond Beta1, this release adds:

  1. Support for Spatial/GIS data through importing Hibernate Spatial.
  2. Complete redesign of bulk id tables used to support update/delete queries against multi-table structures. The redesign helps better fit what different databases support.
  3. Redesign of transaction management
  4. Much improved (and still improving!) schema management tooling for export, validation and migration.

At this point, 5.0.0 is getting a lot of testing. So even though it is still in Beta I am feeling pretty confident of its quality. I opted for another Beta here instead of CR1 for a few reasons:

  1. Investigate whether we want to convert Hibernate's native APIs (Session, etc) to be typed. There is one especially tricky case that needs to be figured out. A major release like this would be the time to do that
  2. I have just introduced some pretty significant Transaction changes since Beta1. I felt it would be prudent to have one more Beta to allow people time to try out those changes and allow for additional changes based on feedback
  3. I would still like to complete deprecating the Settings contract. The last piece there is the discussion I started earlier on the dev list wrt its usage in SPI contracts (L2 cache, etc). This effects a few integrations.
  4. I am working on better Karaf support for hibernate-osgi. Specifically creating a Karaf features respository that users can simply pick up and use. That work is well under way, but ongoing.

As always, see http://hibernate.org/orm/downloads/ for information on obtaining Hibernate O/RM.

4.3.9.Final and 4.2.19.Final were released 15/Apr/2015.

4.2.18.Final was actually released 28/Jan/2015. At the time of that release there were problems with in.relation.to. Unfortunately, after those issues were resolved, the announcement for 4.2.18.Final fell through the cracks, so I am covering that release here as well.

4.2.18.Final and 4.2.19.Final fixes several regressions. Those fixes were also included in 4.3.9.Final.

4.3.9.Final includes mainly bugfixes with some improvements:

  • improved support for AttributeConverters (backported from 5.0.0.Beta1);
  • EntityGraph bugfixes;
  • fixes for load plan bugs that caused problems with CompositeCustomType IDs, embeddable @MapKey, maps containing embeddable keys;
  • fix for a classloading bug that affects Hibernate under GlassFish 4.0 HHH-9446 that was introduced by in 4.3.6 by HHH-8818; by setting an environment variable, Hibernate reverts to pre-HHH-8818 behavior;
  • fix for Envers to work with dynamic (non-POJO) entities.

JBoss Nexus: https://repository.jboss.org/nexus/content/groups/public/org/hibernate
Maven Central: http://repo1.maven.org/maven2/org/hibernate/hibernate-core (should update in a couple of days)

SourceForge: https://sourceforge.net/projects/hibernate/files/hibernate4
4.3.9.Final Downloads: 4.3.9.Final ZIP, 4.3.9.Final TGZ
4.2.18.Final Downloads: 4.2.18.Final ZIP, 4.2.18.Final TGZ
4.2.19.Final Downloads: 4.2.19.Final ZIP, 4.2.19.Final TGZ

Hibernate ORM 5.0.0.Beta1 Release

Posted by Steve Ebersole    |    Mar 31, 2015    |    Tagged as Hibernate ORM Releases

We just got done releasing Hibernate O/RM 5.0.0.Beta1. I will follow up later with a more in depth blog post for 5.0.0.Final, but this first Beta implements all new APIs and most new features. Specifically in this Beta include:

  • New bootstrapping API - better determinism, better integration
  • Java 8 Support (though still compatible with Java 6). Just add hibernate-java8 to your classpath.
  • Ability to handle additional Java types for id attributes marked as GenerationType#AUTO. Built-in support for Number and UUID. Expandable via new org.hibernate.boot.model.IdGeneratorStrategyInterpreter extension
  • scanning support for non-JPA usage
  • NamingStrategy has been removed in favor of a better designed API. 2 actually: org.hibernate.boot.model.naming.ImplicitNamingStrategy and org.hibernate.boot.model.naming.PhysicalNamingStrategy

Additionally, support for AttributeConverters has been expanded and more fully realized:

  • fully supported for non-@Enumerated enum values
  • applicable in conjunction with @Nationalized support
  • called to handle null values
  • settable in hbm.xml by using type="converter:fully.qualified.AttributeConverterName"
  • integrated with hibernate-envers
  • collection values, map keys

See http://hibernate.org/orm/downloads/ for information on obtaining Hibernate ORM.

NOTE: 5.0 is expected to be compatible with Java 6 (aside from the hibernate-java8 module naturally). However, Beta1 will not work with Java 6 due to something with JAXB that I have not yet had time to investigate. Beta1 requires Java 7. Again, the expectation is to have it be compatible with Java 6 by the next release if possible.

These releases contain mostly minor bugfixes and improvements. For more information, please see:

Most changes applied to both versions. Of note are:

  • A new MySQL dialect (MySQL57InnoDBDialect) that adds support for fractional seconds HHH-8401;
  • NamingStrategyDelegator is now supported by standalone schema tools HHH-9467.

Other fixes made to 4.3.8 (only) were minor bug fixes and improvements.

JBoss Nexus: https://repository.jboss.org/nexus/content/groups/public/org/hibernate
Maven Central: http://repo1.maven.org/maven2/org/hibernate/hibernate-core (should update in a couple of days)

SourceForge: https://sourceforge.net/projects/hibernate/files/hibernate4
4.3.8.Final Downloads: 4.3.8.Final ZIP, 4.3.8.Final TGZ
4.2.17.Final Downloads: 4.2.17.Final ZIP, 4.2.17.Final TGZ

You can now upgrade to Version 5.0.0.Beta3 of Hibernate Search, and benefit from the following improvements:

Indexing Performance

We did some further polishing of the shiny new backend improvements introduced by last week. I would be really happy to get some feedback on this, as you should be able to get a very significant performance boost on index writing - whatever the storage technology you're using. We're preparing some large scale tests, but the environments we can test on are limited so I'd be happy if you could send us a note on what your experience with it looks like.

The new design should have a significant improvement in throughput, but also requires less locking, needs less threads and will result into less pressure on GC as it has a lower allocation rate.

JDK9 compatibility

We now have continuous integration running for Java 9 (preview builds) running as well. Except the OSGi integration tests running in Apache Karaf, everything else seems to work fine.

API changes

We're now polishing the API, and it's possible that this might be the last Beta. Two very frequently used interfaces were renamed; please don't miss the Migration Guide.

As always, looking forward for your experience with it! ideas and suggestions on the mailing list or via IRC.

Sanne

back to top