Red Hat

In Relation To Vlad Mihalcea

In Relation To Vlad Mihalcea

Hibernate Community Newsletter 12/2017

Posted by    |       |    Tagged as Discussions Hibernate ORM

Welcome to the Hibernate community newsletter in which we share blog posts, forum, and StackOverflow questions that are especially relevant to our users.

Presentations

Don’t miss the Virtual JUG presentation about High-Performance Java Persistence and Hibernate. If you are using a relational database, then you should definitely attend this session, and, the best thing about it, you can watch it in the comfort of your home.

Articles

The pick of this edition is this article by Arnold Galovics which reiterates the benefits of using projections when fetching data.

JPA inheritance is a very useful addition to the standard. However, sometimes entity inheritance is not very well understood or applied, so, in this series of articles, I tried to offer a different perspective to why we need entity inheritance in the first place, and what is the best way to do it:

Time to upgrade

Hibernate Search has managed to release three final versions:

  • 5.5.7.Final

  • 5.6.2.Final

  • 5.7.1.Final

as well as a 5.8.0.Beta3 release.

Hibernate Community Newsletter 11/2017

Posted by    |       |    Tagged as Discussions Hibernate ORM

Welcome to the Hibernate community newsletter in which we share blog posts, forum, and StackOverflow questions that are especially relevant to our users.

Articles

The pick of this edition is this article by Heap’s Engineering blog which demonstrates the benefits of using batch updates even for reducing database index overhead.

As previously explained, you can speed up integration tests considerably using a RAM disk or tmpfs. Mark Rotteveel‏ tried this approach and looks like it works for Firebird as well.

Hibernate 5.2.10 comes with a very handy connection management optimization for RESOURCE_LOCAL transactions. If you don’t use JTA and you disabled auto-commit at the connection pool level, then it’s worth setting the hibernate.connection.provider_disables_autocommit configuration property as well.

When using Oracle, the fastest way to access a database record is to use the ROWID pseudocoolumn. If using ROWID is suitable for your application, then you can annotate your entities with the @RowId annotation and Hibernate will use the ROWID pseudocoolumn for UPDATE statements.

The best way to manage a database schema is to use incremental update scripts, and a tool like Flyway. Even in this case, you can still benefit from the hbm2ddl tool to validate the entity mappings. Check out how you can deal with schema mismatch exceptions, especially for non-trivial mappings.

You can use Hibernate statistics to log query execution time. However, in reality, many enterprise application are better off using a JDBC DataSource or Driver Proxy which, not only it allows you to log JDBC statements along with their parameters, but you can even detect N+1 query issues automatically during testing.

Presentations

Jakub Kubryński has a very good presentation about JPA common pitfalls and how you should handle them effectively.

Book discount

Until the 1st of June, High-Performance Java Persistence is 33% OFF. Considering the reader testimonials as well as Good Reads and Amazon reviews, it’s quite a deal!

Time to upgrade

  • Hibernate Validator 6.0.0 Beta1 and Beta2 were released.

  • Hibernate ORM 5.1.7 is out, so you should consider updating if you are running the 5.1 version.

Hibernate Community Newsletter 10/2017

Posted by    |       |    Tagged as Discussions Hibernate ORM

Welcome to the Hibernate community newsletter in which we share blog posts, forum, and StackOverflow questions that are especially relevant to our users.

Articles

Mapping JPA relationships is a trivial thing to do. However, not all associations are equal in terms of performance. Check out this series of articles about the best way to map the:

If you’re using TomEE 7, you can easily switch to using Hibernate ORM as the JPA provider. Check out this article which shows you how you can do that, and how you can also speed up application server startup time.

Docker is extremely useful for running database containers that you need when doing integration testing. Check out this article about running IBM DB2 Express-C as a Docker container, and how to set up a JDBC connection to DB2.

Although collections like List and Set are more common when using JPA and Hibernate, you can easily use Maps as explained in this article.

Time to upgrade

Hibernate ORM 5.1.6 has been released, as well as Hibernate Search 5.8.0 Beta 2.

Hibernate Community Newsletter 9/2017

Posted by    |       |    Tagged as Discussions Hibernate ORM

Welcome to the Hibernate community newsletter in which we share blog posts, forum, and StackOverflow questions that are especially relevant to our users.

Articles

Hibernate uses Proxy objects for lazy associations. If you want to understand how Porxy objects work and how you can unproxy them to the actual entities they represent, then you should definitely read this article.

Romain Manni-Bucau wrote a very interesting article which points out how you should be merging incoming and existing association collections when using Hibernate.

Getting access to the underlying database metadata is very easy with Hibernate 5. You just have to know how to make use of the Integrator SPI mechanism, as explained in this article.

Knowing how to implement equals and hashCode is of paramount importance when using JPA and Hibernate. Read this article by Steven Schwenke to find out business keys are very suitable for this task.

Bean Validation is a very convenient mechanism for validating entity state. Check out this article to find out how you can make sure that an integer value is within a bounded range.

Hibernate Community Newsletter 8/2017

Posted by    |       |    Tagged as Discussions Hibernate ORM

Welcome to the Hibernate community newsletter in which we share blog posts, forum, and StackOverflow questions that are especially relevant to our users.

Articles

The Hibernate ResultTransformer is extremely useful to customize the way you fetch data from the database. Check out this article to learn more about this topic.

JPA and Hibernate use the first-level cache as a staging buffer for every read/write operation, and understanding its inner workings is very important if you want to use JPA effectively. For more details, you should definitely read this article.

Marco Behler offers 6 very practical video tutorials for Hibernate beginners.

Dealing with difficult software problems is easier than you might think. Check out this article for more details on how you can solve any software issue with the help of our wonderful software community.

If you wonder why you should choose Hibernate over plain JDBC, this article gives you 15 reasons why Hibernate is worth using.

This very short article offers a quick introduction to mapping a bidirectional one-to-many association. If you want to know what is the best way to map a one-to-many database relationship, then you should definitely read this article as well.

Database concurrency control is a very complex topic. PostgreSQL advisory locks are a very useful concurrency control API which you can use to implement multi-node coordination mechanisms. Check out this article for more details on this topic.

Time to upgrade

Hibernate ORM 5.2.10 has been released, as well as Hibernate Search 5.8.0.Beta1 which is now compatible with ElasticSearch 5.

Hibernate Community Newsletter 7/2017

Posted by    |       |    Tagged as Discussions Hibernate ORM

Welcome to the Hibernate community newsletter in which we share blog posts, forum, and StackOverflow questions that are especially relevant to our users.

Upcoming events

Java Gruppen Danemark is hosting a High-Performance Hibernate webinar. If you want to participate, then you should register on Eventbrite.

Hibernate OGM will be at Devoxx UK 2017. If you want to learn more about Hibernate OGM and Infinispan, then you should definitely come to see Sanne’s presentation.

Articles

A very handy feature when working with JDBC batch updates is to find out which statement triggered a batch failure. Read this article for more info on this topic.

You can use Hibernate with CockroachDB. Check out this tutorial to see how easily you can integrate them.

Mapping the @OneToMany association is not as easy as you might think. Check out this article which shows you the best way to map a @OneToMany relationship.

Alon Segal shows you how to implement Multitenancy with Spring and Hibernate. Multitenancy is very handy when you need to support multiple customers on a single platform so that each customer is limited to its own tenant.

I read this article which describes a way to provide a JPA AttributeConverter to support Java 1.8 Data/Time types. However, this is not needed since Hibernate has been supported them for quite a while now.

For our Czech readers, Roman Pichlík wrote a very good article about all sorts of application performance issues, and the Open-Session in View is mentioned as well.

For our French readers, eXo platform has written an article that shows you how to integrate eXO with JPA and Hibernate.

Time to upgrade

Hibernate Validator 5.4.1 and 6.0.0 Alpha 2 are out.

GORM 6.1 has been released with support for Hibernate 5.2.

Hibernate Community Newsletter 6/2017

Posted by    |       |    Tagged as Discussions Hibernate ORM

Welcome to the Hibernate community newsletter in which we share blog posts, forum, and StackOverflow questions that are especially relevant to our users.

Articles

Implementing the soft delete pattern with Hibernate is trivial. Check out this article for more details.

Sri Vikram Sundar wrote a very detailed tutorial about integrating Spring MVC, MySQL, and Hibernate.

Stefan Pröll wrote two articles about using Hibernate Search and Spring Boot:

Baeldung features an article about using the Hibernate-specific @Immutable annotation to mark entities that should never be modified, which allow Hibernate to enable some flush-time performance optimizations.

For our Portuguese readers, Rafael Ponte wrote a guide to controlling transactions programmatically in Legacy Systems using Java 8 Lambdas and the Template Pattern. For non-Portuguese readers, you can use Google Translate since most Romance languages are easily translated into English.

Time to upgrade

Hibernate ORM 5.1.5 and 5.2.9 have been released.

Hibernate Community Newsletter 5/2017

Posted by    |       |    Tagged as Discussions Hibernate ORM

Welcome to the Hibernate community newsletter in which we share blog posts, forum, and StackOverflow questions that are especially relevant to our users.

Interviews

Don’t miss our Hibernate developer interviews with Marco Pivetta and Kevin Peters.

If you want to share your story about Hibernate, let us know, and we can share it with our huge community of passionate developers.

Books

Javin Paul, a long-time Java blogger, gives a review of the two best Hibernate books for Java developers.

Thorben Janssen is now writing a Hibernate Tips book, and you can get a free copy if you want to review it.

Articles

Nowadays, many RDBMS support JSON column types and Hibernate makes it very easy to use JSON object as entity attributes as this article demonstrates it.

Encrypting and decrypting column values is easy-peasy when using Hibernate. Check out this article for a detailed tutorial on this topic.

Arnold Gálovics wrote a very good article how the LazyInitializationException works in Hibernate.

Craig Andrews is building a Hibernate SpringCache prototype which acts like a Hibernate second-level cache implementation on top of Spring Cache. The idea is very interesting, and we are looking for your feedback on this topic.

Our colleague, Chris Cranford have a talk about Hibernate Performance at DevNexus, and here are the slides.

If you’re using MySQL, then you should know that we refactored the MySQL Dialects so that it’s much easier for you to match a Hibernate Dialect with a given MySQL server version.

Concurrency Control is a very interesting topic, and if you every wondered how MVCC (Multi-Version Concurrency Control) works, then this article is going to unravel how INERT, UPDATE, and DELETE statements work in MVCC-based database engines.

Thorben Janssen wrote two articles about Hibernate Search, one about custom Analyzers and another one about Facets.

Time to upgrade

Meet Kevin Peters

Posted by    |       |    Tagged as Discussions Hibernate ORM Interview

In this post, I’d like you to meet Kevin Peters, a Software Developer from Germany and Hibernate aficionado.

Kevin Peters, align=

Hi, Kevin. Would you like to introduce yourself and tell us a little bit about your developer experience?

My name is Kevin Peters, and I live in Germany where I work as a Software Developer. My first contact with the Java language was around 2005 during my vocational training, and I fell in love with it immediately.

I worked for several companies leveraging Java and Spring to implement ERP extensions, customizing eCommerce systems and PIM solutions. Nearly one year ago, I joined the GBTEC Software + Consulting AG, one of the leading suppliers of business process management (BPM) software, and there we are now reimplementing a BPM system in a cloud-based manner using Dockerized Spring Boot microservices.

You have recently mentioned on Twitter a DataSource proxy solution for validating auto-generated statements. Can you tell us what about this tool and how it works?

We use Spring Data JPA with Hibernate as JPA provider to implement our persistence layer, and we really enjoy the convenience coming along with it. But we also know about the "common" obstacles like Cartesian Products or the N+1 query problem while working with an ORM framework.

In our daily technical discussions and during knowledge transfer sessions we try to raise awareness for these topics among our colleagues, and in my opinion, the best way to achieve this is implementing tests and real world code examples showing that practically.

I started to prepare a small mapping example for one of our technical meetings, called "techtime", to demonstrate the "unordered element collection recreation" issue, and I wanted to show the unexpected amount of queries fired in this simple use case.

Fortunately, I came across the ttddyy/datasource-proxy GitHub project which helped me a lot to make that problem tangible. The datasource-proxy project empowers you to wrap your existing datasource with a proxy and allows you to count all executed queries separated by query type (e.g. INSERT, UPDATE, etc.). With that opportunity you can not only write tests which assert that you are doing the right thing within your use cases, you can also check if you are doing it in an effective way and avoid the traps I did mention before.

At the time when our Coding Architect Ingo Griebsch suggested to use this approach to enhance our test environment by automating the hunt for performance penalties, you caught us talking about your article on Twitter.

Proxies are a great way to add cross-cutting concerns without cluttering business logic. For instance, FlexyPool brings Monitoring and Fallback capabilities to connection pools. Are you using Proxies for other concerns as well, like logging statements?

There are many ways to enrich application code with proxies, facades or aspects. Starting with small things like logging with a facade like SLF4J, using Spring Security for access control, Hystrix service-to-service communication or even "basic" stuff like transactions in Spring Data, all these features are working with proxies, and we won’t miss them anymore.

Why did you choose Hibernate for that particular project, and did it meet your expectations, especially when it comes to application performance?

Hibernate provides a lot of convenience to us, especially if we combine it with Spring Data JPA. But the fact I enjoy most is that you can still switch to Hibernate specific features like Hibernate Named Queries or special Hibernate annotations.

It’s important to know when you can relax using "magic" ORM features and when the opposite is needed - forgo bidirectional relations and write HQL instead or even using database native queries to receive complex data. In our opinion, Hibernate offers the best balance between convenience and performance if one knows how to use it.

Hence, we have a quite complex data model and customers which store a lot of data it’s vital for our software to fetch and write data in a performant way in every of our use cases. And in case of any doubts, at least your articles help us getting things done right.

We always value feedback from our users, so can you tell us what you’d like us to improve or are there features that we should add support for?

In general, we love the feature set of Hibernate. Only the support of UNION HQL queries/Criteria API would be an awesome feature that we missed recently.

Thank you, Kevin, for taking your time. It is a great honor to have you here. To reach Kevin, you can follow him on Twitter.

Meet Marco Pivetta

Posted by    |       |    Tagged as Discussions Hibernate ORM Interview

In this post, I’d like you to meet Marco Pivetta, who is one of the maintainer of Doctrine, a suite of PHP projects that were inspired by Hibernate ORM.

Marco Pivetta, align=

Hi, Marco. Would you like to introduce yourself and tell us a little bit about your developer experience?

I’m Marco "Ocramius" Pivetta, an Italian PHP consultant, currently living in Germany. Yes, the nickname is weird, but it comes from an era of Quake 3 Arena, Unreal Tournament & co.

I’ve been tinkering with computers since I was a child, and have been working with PHP for more than half my life now, developing a love-hate relationship with the language. Interestingly, I didn’t start with the usual Joomla/Wordpress/Drupal/etc, but built a quite complex website that interacted with a browser game called "OGame", and scraped game information through a Firefox addon that would then provide an additional information to the players.

The reason why this project ("stogame") is important for me is that it included extremely challenging problems to be solved for a rookie with no help at all, and is still one of the most complex projects that I worked on:

  • XSS/SQL injections - had those, wasn’t fun

  • queuing mechanisms to sync browser extensions and the website - invented my own system

  • optimizing queries and indexes on ~60Gb of MySQL MyISAM tables

  • disaster recoveries on such a system - had those too, wasn’t fun either

  • real-time push mechanisms for clients via BOSHXMPP

  • simplistic prediction engine to aid players in decision making

All of the above were built by 15-years-old-me by just spending countless sleepless nights on it, and also jeopardizing my school evaluations. Still, this was before libraries, design patterns, mentoring, Github: only me, some friends, and a good amount of design and prediction work.

I then moved on, gave up on the project, failed university (I’m a terrible student), got a few jobs and started using frameworks. Eventually, I got to work with all of the typical DB abstraction approaches:

  • Active Record (with ZendFramework)

  • Table Data Gateway - in a custom solution

  • Data Mapper - in a Java EE project

I liked the JPA approach in the Java EE project so much that I started looking for a PHP analogue solution for my daytime job, and ended up discovering Doctrine 2.

Since then, I started getting more and more involved with the project, starting from answering questions on the mailing list and StackOverflow. Benjamin Eberlei, who was the lead on the project at that time, pushed me towards contributing with actual code changes back in 2011.

Eventually, I became part of the maintainers of the project, and that also boosted my career, allowing me to become a consultant for Roave, which allows me to see dozens of different projects, teams and tools every month, as well as a public speaker.

You are one of the developers of Doctrine ORM framework. Can you please tell us what’s the goal of Doctrine?

I am actually not one of the developers, but one of the current maintainers. The initial designers of the current Doctrine 2 ORM, as far as I know, are Jonathan Wage, Guilherme Blanco, Benjamin Eberlei and Roman Borschel. I can probably still answer the question: Doctrine ORM tries to abstract the "database thinking" away from PHP software projects, while still being a leaky abstraction on purpose.

To clarify, most PHP developers are used to developing applications from the database up to the application layer, rather than from the domain logic down, and that’s a quite widespread problem that leads to hardly maintainable and unreadable code. This tool gets rid of most of those problems, by still allowing developers to access the database directly when needed.

Ruby on Rails employs the Active Record pattern. Why did Doctrine choose the ORM paradigm instead?

Interestingly, Doctrine 1.x was an Active Record library, and also a quite good one, but it became evident quite quickly that the JPA specification and Data Mapper plus Unit of Work were better solutions altogether.

Specifically, the Data Mapper approach allows consumers of the library to write abstractions that decouple the tool from the domain almost completely (there are always limitations to this). The Unit of Work pattern has an increased memory impact for PHP applications, but also massively reduces required query operations (via in-memory identity maps) while adding some transactional boundaries, and that is a big win for most PHP apps, which often don’t even use transactions at all.

There are more advantages, but I personally wouldn’t ever consider using Active Record again due to its limitations and inherent framework coupling. This doesn’t mean that Active Record doesn’t work, but I’ve been burnt many more times with AR than with DM.

Since Hibernate ORM has been influencing Doctrine, can you tell use the similarities and differences between these two frameworks?

Doctrine is hugely inspired by Hibernate and the JPA, although we couldn’t really copy things, both due to licensing issues and life-cycle differences in Java and PHP software.

Doctrine resembles Hibernate in the Unit of Work, mappings, basic event system, second level cache and the DQL language (HQL in Hibernate). We even designed an annotation system for PHP, since the language doesn’t support them, and it currently is the de-facto standard for custom annotations in PHP libraries, and we initially only needed this to simulate inline mappings like Hibernate allows them.

Where things differ a lot are flexibility and lifecycle, since Java is an AOT-compiled language with a powerful JIT and generally deployed in long-running applications.

PHP is an interpreted language, and its strength is also its pitfall: the typical share-nothing architecture allows for short-lived, memory-safe, retry-able application runs. That also means that we have no connection pooling, and the ORM internals are much more inflexible and less event-driven than Hibernate’s due to memory and execution time constraints. That also means that we rarely encounter memory issues due to large Unit of Work instances, and connections and entity instances aren’t shared across separate web application page loads, and slow ORM will unlikely slow down an entire application server.

Another huge difference is managed state: DETACHED makes little sense in the PHP world, since a detached entity may only come from serialized state. In Doctrine 3.x, we are planning to remove support for detaching entities, since storing serialized objects in PHP is generally leading to security issues and more trouble.

As you can see, the differences are indeed mostly in the lifecycle, but each language and framework has its strengths and pitfalls.

We always value feedback from our users, so can you tell us what you’d like us to improve or are there features that we should add support for?

I’m probably being weird here, but I don’t lack any particular features from either ORM at this time. What would be interesting is reducing support for entity and transaction lifecycle events, since most consumers of these ORMs tend to code application and domain logic in those, while they were mostly intended for technical tasks, such as creating audit logs and executing pre- and post- DB cleanup tasks.

A possible improvement is to explore saving/loading of single aggregate-root-acting entities attached to a Unit of Work, which is only responsible for tracking state in child aggregates. This is only to prevent sharing entity references across aggregates, and to prevent DB transactions from crossing aggregate root boundaries.

Thank you, Marco, for taking your time. It is a great honor to have you here. To reach Marco, you can follow him on Twitter.

back to top