lightweight ORM instead of hibernate - robust and agile [closed]

给你一囗甜甜゛ 提交于 2019-12-03 20:15:56

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

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#
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!