filo/DigitalVision Vectors via G

Tip

5 Java ORM tools to know

ORMs are a popular method for connecting to databases from Java. Consider the tradeoffs of these ORM tools to decide which tool to run with.

Object relational mappers, which are code libraries that connect object-oriented code to relational databases directly, serve as an alternative to SQL in some instances.

ORMs are attractive because they enable the programmer to keep programming in Java without having to learn yet another "glue" language, such as SQL, or to deal directly with the pesky database. Experts have pointed out that isn't always a good thing, as these tools have limitations over pure SQL and might have performance problems. Still, Java ORMs can be useful for abstracting database interactions and reducing boilerplate code.

Should a developer elect to use an ORM in Java, they have several tools to choose from, each with notable pros and cons.

How to connect to relational databases in Java

There are three common ways to connect to a relational database in Java:

  1. Using SQL with Java Database Connectivity. The JDBC standard is essentially a code library. With JDBC, developers combine a connection string -- a URL for the database -- with a username and password to create a session object and then send SQL code directly, which returns a collection, such as a ResultSet.
  2. Indirectly, through web APIs. Web APIs are less common, as Java tends to be server-side. If anything, they connect to the database and serve up the web API. Plenty of Android applications are written in Java and call web APIs. Some APIs written in Java can call others, such as the API gateway pattern.
  3. Through the objects themselves, with something such as Jakarta Persistence API. JPA is an interface standard for object relational mapping. JPA is not an implementation; it is a representation of how one uses an ORM. Because there is a set of standard ways ORMs work, they are easy to use and less difficult to switch between.

Advantages of ORMs in Java

ORMs, especially those that implement JPA, tend to use plain old Java objects (POJO), which have annotations and configuration behind the scenes to connect the objects to the database. That means the programmer doesn't need to know SQL and can have something resembling automatic serialization to and from the database built in for free. ORMs generate the SQL again behind the scenes and can, in some cases, even manage when commits happen.

By managing the state of the database, ORMs can reduce the number of trips to and from the database, reducing the load and increasing performance.

They can load data just in time or load a great deal of data early, creating a sort of caching effect. By managing the state of the database, ORMs can reduce the number of trips to and from the database, reducing the load and increasing performance. That lack of fine-grained control over the database is remarkably also the downside of ORMs, as programmers might feel they lack control and debugging can become more difficult.

While most ORM tools either let the programmer "drop into SQL" to perform queries or have their own queries, some let the user avoid SQL entirely. Many tools, such as Hibernate, are connected to a database and can create all the entities in the database for the user. Hibernate is even configurable, so it can plan to not create the table, create it only if it does not exist, or plan to add the table and return an error if the table exists.

5 Java ORM tools to know

When working in Java, developers have a range of ORM tools at their disposal, each offering unique features, advantages and disadvantages that cater to different project requirements. Consider these five noteworthy ORM tools for Java projects.

The author compiled this list using a combination of internet research, discussions with experts and the author's own experience. Starting with JPA, the interface standard for serialization, the author looked for JPA implementations as object relational mapping. This leads to Hibernate, which is widely held as a reference standard for JPA implementations. Discussions with David Hoppe, a consulting advisor with Excelon, led to Spring, which is the context under which Hibernate is most commonly used. All this matched the author's own experience. Beyond Spring and Hibernate, internet research led the author to a variety of "most popular" and "best" ORM pages. ORMs were selected for a combination of existing reputation, state of public documentation, internet references, visible community support, common discussion, and integration and affiliation. The list is in alphabetical order.

EclipseLink

EclipseLink is relevant because of its connection to Eclipse, which, according to the JRebel "2023 Java Developer Productivity Report," is the second most popular Java IDE. EclipseLink is an open source framework and has support for XML but also some legacy databases and many NoSQL databases. Developers can map objects to databases through annotations and store the mappings in XML through the EclipseLink MOXy (Mapping Objects to XML) component. EclipseLink also has support for Service Data Objects.

Hibernate

Starting in 2001 as an alternative to Enterprise Java Beans for persistence, Hibernate quickly became the de facto open source ORM tool for Java. At the least, it is the tool to compare others against. With Hibernate, there is still a concept of a session, and the session has a state. Thus, developers can create new objects with new(), configure them, call session.save() and then call .getTransaction().commit() on the session.

Hibernate has lazy loading and eager loading, which are configurable down to individual objects, so developers can eager-load lookup tables and lazy-load large tables, such as claims or orders.

ObjectDB

While there are few benchmarks for performance with an ORM, ObjectDB Software has created their own, the JPA Performance Benchmark. While other JPA implementations work with any database, ObjectDB ties to its own database, either an embedded version -- a single user database that belongs to the application user -- or a server-based version. According to the benchmark tests, ObjectDB scores exceptionally, on the order of five to 10 times the performance speed of any competitor. It does provide a basic benchmark page that describes all the tests, including the exact size of the machine, OS, amount of disk, memory, type and speed of CPU. The results are impressive but may require either a conversion from the old database to ObjectDB or a special side project where the data can be stored.

Spring over Hibernate

Spring is an open source framework that adds many features to standard Java, such as dependency injection and the model-view-controller framework. As for its ORM aspects, Spring adds the ability to manage complex transactions through many data access methods. That means a legacy app could use Spring TransactionManager for both JDBC and Hibernate.

More importantly, the @transactional annotation enables developers to make the commit operation look just like Java code. Spring supports both POJOs and Java Beans. Spring Boot is a part of the same open source family as Spring and adds default templates to create web applications.

TopLink

While Hibernate may be the predominant open source tool for JPA and Java, the language of Java was created by Sun Microsystems, which is not the property of Oracle. However, Oracle maintains and supports the Java virtual machine, and TopLink is Oracle's implementation of JPA.

Because JPA runs over JDBC, TopLink supports essentially all modern relational databases. In addition, TopLink has support for converting XML behind the scenes into objects and then back again, all over the JPA standard. That makes changes to complex configuration files as easy as manipulating POJOs within the context of a transaction. ORMs tend to run into problems as the scope of the database becomes too large or if there are many complex relationships between tables. XML configurations tend to avoid these problems.

How to pick a Java ORM tool

In summary, Hibernate and Spring might be best for avoiding vendor lock-in. Toplink, EclipseLink and ObjectDB are best because of vendor lock-in, especially considering close ties to Java server technologies, such as WebSphere, WebLogix and JBoss. These technologies are still popular and are embedded deeply into organizations. They often require complex XML configurations to tie databases to servers to application code. If tied to an application server, carefully consider the ORM with the best connection to that application server, but also consider switching cost.

Matt Heusser is managing director at Excelon Development, where he recruits, trains and conducts software testing and development. The initial lead organizer of the Great Lakes Software Excellence Conference and lead editor of How to Reduce the Cost of Software Testing, Heusser served a term on the board of directors for the Association for Software Testing.

Dig Deeper on Software design and development

Cloud Computing
Search App Architecture
Search ITOperations
TheServerSide.com
Search AWS
Close