Question

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

Was it helpful?

Solution

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 :)

OTHER TIPS

I wasn't going to answer this question until I read @Xorty's post. 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 marry any 3rd party solution -- especially one as critical as ORM.

Hope this helps. Best of luck.

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.

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#
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top