Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Java ORM do you prefer, and why? [closed]

Tags:

java

orm

People also ask

What is an ORM and why you should use it?

What is an ORM? An object-relational mapper provides an object-oriented layer between relational databases and object-oriented programming languages without having to write SQL queries. It standardizes interfaces reducing boilerplate and speeding development time.

Should you use ORM in Java?

So, do you need an ORM? If you have any more or less complex project and you work with a relational database, then yes, definitely. Big ORMs seem "bloated" not because they are bad tools, but rather because the underlying problem of object-relational mapping is hard.


I have stopped using ORMs.

The reason is not any great flaw in the concept. Hibernate works well. Instead, I have found that queries have low overhead and I can fit lots of complex logic into large SQL queries, and shift a lot of my processing into the database.

So consider just using the JDBC package.


None, because having an ORM takes too much control away with small benefits. The time savings gained are easily blown away when you have to debug abnormalities resulting from the use of the ORM. Furthermore, ORMs discourage developers from learning SQL and how relational databases work and using this for their benefit.


Many ORM's are great, you need to know why you want to add abstraction on top of JDBC. I can recommend http://www.jooq.org to you (disclaimer: I'm the creator of jOOQ, so this answer is biased). jOOQ embraces the following paradigm:

  • SQL is a good thing. Many things can be expressed quite nicely in SQL. There is no need for complete abstraction of SQL.
  • The relational data model is a good thing. It has proven the best data model for the last 40 years. There is no need for XML databases or truly object oriented data models. Instead, your company runs several instances of Oracle, MySQL, MSSQL, DB2 or any other RDBMS.
  • SQL has a structure and syntax. It should not be expressed using "low-level" String concatenation in JDBC - or "high-level" String concatenation in HQL - both of which are prone to hold syntax errors.
  • Variable binding tends to be very complex when dealing with major queries. THAT is something that should be abstracted.
  • POJO's are great when writing Java code manipulating database data.
  • POJO's are a pain to write and maintain manually. Code generation is the way to go. You will have compile-safe queries including datatype-safety.
  • The database comes first. While the application on top of your database may change over time, the database itself is probably going to last longer.
  • Yes, you do have stored procedures and user defined types (UDT's) in your legacy database. Your database-tool should support that.

There are many other good ORM's. Especially Hibernate or iBATIS have a great community. But if you're looking for an intuitive, simple one, I'll say give jOOQ a try. You'll love it! :-)

Check out this example SQL:

  // Select authors with books that are sold out
  SELECT * 
    FROM T_AUTHOR a
   WHERE EXISTS (SELECT 1
                   FROM T_BOOK
                  WHERE T_BOOK.STATUS = 'SOLD OUT'
                    AND T_BOOK.AUTHOR_ID = a.ID);

And how it can be expressed in jOOQ:

  // Alias the author table
  TAuthor a = T_AUTHOR.as("a");

  // Use the aliased table in the select statement
  create.selectFrom(a)
        .whereExists(create.selectOne()
                           .from(T_BOOK)
                           .where(T_BOOK.STATUS.equal(TBookStatus.SOLD_OUT)
                           .and(T_BOOK.AUTHOR_ID.equal(a.ID))))));

Hibernate, because it's basically the defacto standard in Java and was one of the driving forces in the creation of the JPA. It's got excellent support in Spring, and almost every Java framework supports it. Finally, GORM is a really cool wrapper around it doing dynamic finders and so on using Groovy.

It's even been ported to .NET (NHibernate) so you can use it there too.


Hibernate, because it:

  • is stable - being around for so many years, it lacks any major problems
  • dictates the standards in the ORM field
  • implements the standard (JPA), in addition to dictating it.
  • has tons of information about it on the Internet. There are many tutorials, common problem solutions, etc
  • is powerful - you can translate a very complex object model into a relational model.
  • it has support for any major and medium RDBMS
  • is easy to work with, once you learn it well

A few points on why (and when) to use ORM:

  • you work with objects in your system (if your system has been designed well). Even if using JDBC, you will end up making some translation layer, so that you transfer your data to your objects. But my bets are that hibernate is better at translation than any custom-made solution.
  • it doesn't deprive you of control. You can control things in very small details, and if the API doesn't have some remote feature - execute a native query and you have it.
  • any medium-sized or bigger system can't afford having one ton of queries (be it at one place or scattered across), if it aims to be maintainable
  • if performance isn't critical. Hibernate adds performance overhead, which in some cases can't be ignored.