3

i googled the phrase "lightweight ORM for j2ee" and find this page http://java-source.net/open-source/persistence from one of results. my goal is to find an ORM Framework that is lighter than Hibernate and also delivers some of hibernates features that are most important to me, for example: auto table generating and lazy initializing, and don't give me hard time with tables and maps and collection. it is also important that the coming ORM has a community around it which makes it faster to find solutions to errors and bugs. and it also is important that the new orm hides the database from me (needs less sql skills and be more OO). so far i have narrowed my choices to iBatis (myBatis) and ORMLite. i want this ORM for my new project which is a desktop application in j2ee, so it is important that the start up time for this orm be less than others (unlike Hibernate which takes a lot of time for first time running specially when you have lots of tables)

thnx

MoienGK
  • 4,544
  • 11
  • 58
  • 92
  • Related to questions: http://stackoverflow.com/questions/5489799/fast-simple-to-learn-and-pojo-friendly-orm-for-java, http://stackoverflow.com/questions/3491540/java-orm-for-a-read-only-db, and http://stackoverflow.com/questions/450864/lightweight-java-persistence – Gray Sep 11 '11 at 13:23
  • Most closely related to http://stackoverflow.com/questions/296587/light-weight-alternative-to-hibernate – Gray Sep 11 '11 at 13:27
  • thnx, I've read them all, there were not what i had in mind, but so far I've found ibatis – MoienGK Sep 11 '11 at 17:52

4 Answers4

13

my goal is to find an ORM Framework that is lighter than Hibernate and also delivers some of hibernates features that are most important to me

I wrote ORMLite as a lightweight replacement for Hibernate and I disagree with the notion that only Hibernate is a "proper ORM" and the only other alternative is to configure Hibernate correctly or not use ORM libraries at all. We use Hibernate at work and yet I found it too heavy when I went to use it in an outside project. I have a big Android user base because Hibernate on a mobile device is just major overkill.

Certainly hibernate has more/better features than ORMLite but it also is fatter and has more dependencies. Every different ORM solution (and there are many) has a different feature set, dependencies, speed, and overall cost/benefit. Each development project should evaluate their needs before they get attached to any 3rd party solution – especially one as critical as ORM.

Hope this helps. Best of luck.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • What you don't mention on ormlite.org's start page is what ormlite is not. Perhaps it's worth mentioning that not all of javax.persistence is supported, such as cascading updates etc. – jontejj Aug 15 '14 at 07:24
8

I am afraid that you might have misunderstood concepts. You basically aren't looking for alternative to Hibernate, but an alternative to ORM.

Hibernate is one of the very few attempts of proper ORM. Hibernate tries to solve most of of paradigms of object/relational mismatch. Those are:

  • problem of granularity
  • problem of subtypes
  • problem of identity
  • problems relating to associations
  • problem of data navigation

See Java Persistence with Hibernate for more information.

To sum up, there is no such thing as lighter ORM. There is either proper ORM solution, or other solutions - like myBatis, where instead of mapping relational model to object model you map SQL statements to objects and methods.

And don't forget - you don't have to use all of Hibernate features. You can quite comfortably mix it with custom SQL, plain JDBC and use only subset of it's capabilities.

edit1: about slow startup

Intermezzo: I am currently working with one propietary ORM solution (not as smart as Hibernate tough) whitch was tuned specially for our application. The biggest issue is also startup, that's becuase mapping the whole database to objects simply isn't trivial.

Now, as for Hibernate. Maybe you know, that Hibernate also generates CRUD SQL statements on startup. If you have big database this can impact performance. But, you can turn off this startup SQL generation and switch to dynamic statements generated at runtime.

Using XML notation, it can be achieved like this:

<class name="SomeClass"
    dynamic-insert="true"
    dynamic-update="true">
...
</class>

Or with Hibernate annotations:

@Entity
@org.hibernate.annotations.Entity(
    dynamicInsert = true, dynamicUpdate = true
)
public class SomeClass { ...

edit2: about mixing custom SQL

The referenced book Java Persistence with Hibernate explains things rather in depth. Chapter 8 is about working with legacy databases and it also gives hints how to alter DML (like with custom SQL, you can even replace CRUD code with custom SQL!) and DDL(generic runtime DDL manipulation). You should peek there :)

Xorty
  • 18,367
  • 27
  • 104
  • 155
  • i am using hibernate for about a year, i know that hibernate is an ORM, but some short comes like being slow on start up (that in this case is very important to me) forced me to search for an alternative for HIBERNATE, IMHO hibernate is the one ORM for industrial uses, so the phrase "alternative for hibernate" is not wrong. – MoienGK Sep 11 '11 at 17:57
  • "And don't forget - you don't have to use all of Hibernate features. You can quite comfortably mix it with custom SQL, plain JDBC and use only subset of it's capabilities." how can i do this? can you give me hint to start? – MoienGK Sep 11 '11 at 18:04
  • @dave I edited answer, see more info there – Xorty Sep 11 '11 at 18:22
  • thnx, very helpful , specially first edit part, i will give it a try, – MoienGK Sep 11 '11 at 19:12
  • so in conclusion do you suggest to tune hibernate and use it, instead of migrating to a new ORM, like myBatis? you know that this is much easier to me, because i am much familiar with hibernate. – MoienGK Sep 11 '11 at 19:15
  • Again, I don't think of myBatis as ORM. Well, Hibernate is not ideal for desktop application. Hibernate applications, as you probably know, usually live on servers. I personally would invest time to protyping (use large database) and see if you gain reasonable performance gain with myBatis (you can for example try to load big grids of data to GUI at startup and so on). IF you do, THEN start implementing business logic with myBatis. – Xorty Sep 11 '11 at 19:23
0

If you are looking for a lightweight ORM I would recommend jORM. Note that my answer is biased, since I'm one of the contributors to the project.

The main reasons why we decided to write a lightweight alternative are (the need for):

  • Hazzlefree configuration
  • Clear transaction definitions
  • Memory management and speed
  • Simple code generation

In short; rapid development.

Example configuration

BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/moria");
dataSource.setUsername("gandalf");
dataSource.setPassword("mellon");
Database.configure("moria", dataSource); // now there's a named database

Example mapped queries

Goblin goblin = Record.findById(Goblin.class 42);
Goblin bolg = Record.find(Goblin.class, new Column("name", "Bolg"));
List<Goblin> goblins = Record.findAll(Goblin.class);

Example custom query

Tribe tribe = new Tribe();
tribe.setId(1);
String name = "Azog";
Goblin azog = Record.select(
    Goblin.class,
    "SELECT * FROM goblins WHERE name = #1# AND tribe_id = #2:id#",
    name, // #1#
    tribe // #2#
);
Martin
  • 2,347
  • 1
  • 21
  • 21
0

How about mybatis: http://www.mybatis.org/

It is lighter than hibernate but still has the features you desire. It is a well established framework (a while ago it was called ibatis, but it exists for years now). The Alfresco developers have decided that for their 4th version of the Alfresco CMS they'll switch from hibernate to ibatis for performance reasons mostly.

Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103
  • Mybatis doesn't hide the database or require less SQL skills. Not to say it's not a good framework, but it doesn't sound like it does what the OP wants. – Nathan Hughes Sep 11 '11 at 12:55
  • yep, that's one of my options as i mentioned above, but they say the community around it is not mature enough! and they also say that it is not hiding database as well as hibernate does. i have just read these online. – MoienGK Sep 11 '11 at 12:59
  • 1
    Well, not to be a crapper, but if you chose an ORM because it hides the SQL so that you have minum to none interaction with it, you won't get very far in your project based on that. If you want a simpler persistance solution with a nice Java OO like API and you don't care about SQL, there are other not-SQL-DB solutions out there. How about a nice opensource LDAP server like open ldap, and Spring-LDAP for the Java ODM (object directory mapping, as the spring framework folks call it)? Or a nosql database. – Shivan Dragon Sep 11 '11 at 13:29
  • hmm, seems nice, let me look around for ldap – MoienGK Sep 11 '11 at 19:45
  • Try openldap or ApacheDS, both free good products. Get Apache Directory Studio as well (stand alone or eclipse plugin) as LDAP Browser. Then check out http://www.springsource.org/ldap, it has some nice examples and great documentation – Shivan Dragon Sep 11 '11 at 19:51
  • i searched a little bit about ldap and ApacheDS, many pages talk about ldap and network and internet and ip and ... , this raise a question for me : is ldap solutions suitable for desktop and local applications? sorry this question may sound DAAHH! to you :D – MoienGK Sep 11 '11 at 20:17