118

I have a single user java program that I would like to have store data in a light weight database such as Derby or Sqlite. I would like to use a data abstraction layer in my program. Hibernate appears to require a lot of configuration and is overkill for what I need. What are light weight alternatives to Hibernate?

Jared
  • 39,513
  • 29
  • 110
  • 145
  • 2
    You can check alternatives along with example usage (and example configuration with Spring) here: https://github.com/bwajtr/java-persistence-frameworks-comparison – Břetislav Wajtr Dec 30 '16 at 19:26
  • CMobileCom JPA is a light-weight JPA implementation for both Java and Android. Its size is about 380K. It is light and fast. https://cmobilecom.com – John Sep 03 '18 at 04:35

14 Answers14

141

Hibernate requires next to zero configuration if you use annotations. It even can auto-discover mapped beans in the classpath! I don't see any alternative to it from both simplicity and power point of view.

It can also expose itself as JPA, which is (IMHO) even simpler.

Yishai
  • 90,445
  • 31
  • 189
  • 263
Vladimir Dyuzhev
  • 18,130
  • 10
  • 48
  • 62
  • 38
    next to zero is not zero. – njzk2 Aug 05 '11 at 10:10
  • 13
    also it needs a lot external libraries try to use it on android :P – sherif Oct 23 '11 at 13:12
  • 4
    I tried to find a simpler alternative to Hibernate. In a personal project, I used MyBatis (what a terrible name). It was much more work than Hibernate. Hibernate really saves a lot of SQL coding and helps with parent-child relationships. I also looked at a few ActiveRecord style ORM's for Java. None seemed mature or any easier than Hibernate. So, I'm going back to Hibernate. – devdanke Mar 16 '12 at 13:26
  • 62
    was the question not about an ALTERNATIVE? – Łukasz Gruner Mar 16 '13 at 12:02
  • 1
    Hibernate works pretty well with Derby - it was very simple to migrate my Eclipse RCP application fromSQL Server to embedded Derby. If you're using annotations the amount of configuration is in fact quite small. – muriloq May 14 '09 at 18:28
  • 11
    Hibernate is not a lightweight alternative to Hiberernate – cosbor11 Oct 21 '15 at 06:13
  • Using direct mapping with the objects assumes this can be done, using inversion the mapping xml is pretty heavy with hibernate. Hibernate can also be problematic with its proxy objects that also add objects onto the heap. Often a simpler alternative is required simply using JdbcTemplate and a field mapping strategy. – Brett Ryan Apr 16 '16 at 13:58
  • If you want to use Hibernate in android app, it is too heavy obviously. Now there is a light weight JPA implementation available for Android and Java - "CMobileCom JPA". It is light-weight(about 380K) and fast. Check it out at https://cmobilecom.com. – John Sep 03 '18 at 04:43
  • Hibernate is not lightweight by any measure. I actually wrote about it some time ago: http://igorpolevoy.blogspot.com/2012/04/just-how-thin-can-framework-be.html – ipolevoy Jun 02 '21 at 20:42
58

My ORMLite library is one such alternative. It supports MySQL, Postgres, Microsoft SQL Server, H2, Derby, HSQLDB, and Sqlite, and can be easily extended to others. It uses annotations to configure classes, good Spring support, flexible query builder, etc..

Gray
  • 115,027
  • 24
  • 293
  • 354
  • 2
    OrmLite really looks promising! But is this project still alive? No new version for over a year, no answer to pull requests/issues on Github. – Stefan May 17 '16 at 08:47
  • 3
    Version 5.0 was released in June 2016. – Lauri Laanti Aug 18 '16 at 14:21
  • Version 5.1 was released on 2/19/2018. But is not comparable to Hibernate at all and some JPA annotation implementation doesn't work properly. – M-Razavi Jul 12 '19 at 20:08
  • 1
    "comparable to Hibernate"? They are both ORMs but otherwise vastly different. In terms of JPA implementations, pls fill out a bug report if you find something @M-Razavi. – Gray Jul 15 '19 at 22:11
32

It still requires XML configuration, but have a look at MyBatis (formerly iBatis).

M-Razavi
  • 3,327
  • 2
  • 34
  • 46
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • 1
    Now it's MyBatis that's left: http://www.mybatis.org/ – Vladislav Rastrusny Mar 30 '11 at 17:16
  • 3
    I think the XML in i/MyBatis is actually an advantage since it keeps complicated queries in a format which can be easily copied to an SQL console for testing. – Peter Tillemans Jun 22 '11 at 21:44
  • 1
    iBatis is very lightweight. You can more easily define and control your SQL to object mapping. – Berlin Brown Nov 18 '08 at 22:37
  • ... have to throw a vote for iBatis, though mind you it's not an ORM alternative as it is not trying to compete with likes of Hibernate. It's a bit different sort of an animal, but fits what you're looking for in terms of weight. – vector Nov 12 '09 at 04:43
24

jOOQ ships with a fluent DSL simulating SQL directly in Java as a side-effect for its main goals which are:

  • Source code generation
  • Full support for standard SQL including SQL language features such as UNIONs, nested SELECTs, all types of JOINs, aliasing (e.g. for self-joins), etc
  • Wide support for non-standard SQL including UDT's, stored procedures, vendor-specific functions, etc.

Read about jOOQ in this article: http://java.dzone.com/announcements/simple-and-intuitive-approach, or visit the website directly: http://www.jooq.org

(Disclaimer, I work for the company behind jOOQ)

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • 3
    jOOQ rocks! We implemented a large scale ETL service with it, and couldn't be happier. I've used iBATIS, and jOOQ gives all the same advantages of low level SQL without the reams of XML and lack of type safety. – spieden Mar 02 '12 at 02:16
  • 3
    I forgot to mention one of the best features of jOOQ: Complex and/or dynamically generated queries can be assembled in a controlled way and verified by the Java compiler, saving you from all those ambiguous syntax errors databases throw, and providing compile-time regression coverage for when the schema changes under you. – spieden Mar 02 '12 at 02:26
  • JOOQ is amazing but obviously its now not free to Oracle and Sybase so lot of people are avoiding this framework and are using like MyBatis – Makky Oct 03 '14 at 18:44
  • @Makky: Thanks for your feedback. I do hope that you're keeping actual value propositions in mind, and that you're not going to be switching to a completely different framework just based on the fact that one is free and the other is not. The cost of maintenance and migration this would induce in *your* software is prohibitive and far beyond any reasonable commercial license... – Lukas Eder Oct 04 '14 at 14:21
16

Apache Commons DBUtils takes much of the repetitive gruntwork out of JDBC programming. It requires little configuration and is easy to learn. It is not an ORM framework (in the way that Hibernate and other frameworks mentioned here are) but it does automate mapping of SELECT columns to Java member fields as well as other repetitive JDBC programming tasks. It's certainly lightweight.

12

You can have a look at Ebean ORM. - No sessions - lazy loading just works - Simpler API to use and learn.

Community
  • 1
  • 1
Rob
  • 141
  • 1
  • 2
9

Cayenne has served me well. Relatively easy to understand and to get it up and running. I find the reverse engineering part particularly charming. Configuration can be done with a GUI.

Benno Richters
  • 15,378
  • 14
  • 42
  • 45
6

I can propose apache empire-db. http://incubator.apache.org/empire-db/

Apache Empire-db is an Open Source relational data persistence component which allows database vendor independent dynamic query definition as well as safe and simple data retrieval and updating. Compared to most other solutions like e.g. Hibernate, TopLink, iBATIS or JPA implementations, Empire-db takes a considerably different approach, with a special focus on compile-time safety, reduced redundancies and improved developer productivity.

An example:

// Define the query
DBCommand cmd = db.createCommand();
DBColumnExpr EMPLOYEE_FULLNAME= db.EMPLOYEES.LASTNAME.append(", ")
                        .append(db.EMPLOYEES.FIRSTNAME).as("FULL_NAME");
// Select required columns
cmd.select(db.EMPLOYEES.EMPLOYEE_ID, EMPLOYEE_FULLNAME);
cmd.select(db.EMPLOYEES.GENDER, db.EMPLOYEES.PHONE_NUMBER);
cmd.select(db.DEPARTMENTS.NAME.as("DEPARTMENT"));
cmd.select(db.DEPARTMENTS.BUSINESS_UNIT);
// Set Joins
cmd.join(db.EMPLOYEES.DEPARTMENT_ID, db.DEPARTMENTS.DEPARTMENT_ID);
// Set contraints and order
cmd.where(EMP.LASTNAME.length().isGreaterThan(0));
cmd.orderBy(EMP.LASTNAME);;
Somatik
  • 4,723
  • 3
  • 37
  • 49
3

I might be a bit late to the party, but I released ActiveJDBC in 2010, which is an ORM implementation of ActiveRecord pattern, is more than 10 times lighter than Hibernate in dependencies, at least twice as fast at run time, and requires zero configuration or annotations.

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
3

ORMAN framework is also good. https://github.com/ahmetalpbalkan/orman

Documentation: https://github.com/ahmetalpbalkan/orman/wiki

Comparison: https://github.com/ahmetalpbalkan/orman/wiki/Why-orman-is-better-than-other-orms-for-you%3F

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
1

You might want to take a look at prevayler (on sourceforge). A somewhat more lightweight approach to persistence. Or were you thinking about doing reporting against the DB?

Stephan Eggermont
  • 15,847
  • 1
  • 38
  • 65
1

If using a relational database is not mandatory, give db4o a try.

Behrang
  • 46,888
  • 25
  • 118
  • 160
1

I created sormula as an alternative to heavyweight ORM's. It is CRUD-ready, POJO-friendly, simple to use, configure, and understand. Zero-configuration use is possible. www.sormula.org

Jeff Miller
  • 1,424
  • 1
  • 10
  • 19
-2

Kiteframework is also very light orm framework. It provides almost all db operation with minimal configurations.

http://deipakgarg.github.com/Kite-ORM/

Disclosure: I am the author of this project

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • 3
    Welcome to Stack Overflow! Thanks for posting your answer! Please be sure to read the [FAQ on Self-Promotion](http://stackoverflow.com/faq#promotion) carefully. Also note that it is *required* that you post a disclaimer every time you link to your own site/product. – Andrew Barber Nov 19 '12 at 18:36
  • 2
    it's not in development, no new current release from the author, last expected release was in 2012. – Barun Aug 17 '15 at 06:59