views:

314

answers:

4

I am begining a new project that i think will last for some years. Am in the point of deciding the ORM framework to use (or whether to use one at all). Can anyone with experience tell me whether orm frameworks are used in realworld applications. The problem i have in mind is this: The orm tool will generate for me tables and columns etc as i create and modify my entities. However, after the project has gone live and is in production, certain database changes will not be possible. Can this hinder the advancement of the project. If i had used a framework like ibatis for example, i know i would only need to adjust the sql statements based on the database changes. Can someone tell me whether ORM tools have survived the live environment. At my office, we use java based ERP that was done long ago and it was never done using any ORM framework.

Regards. Josh

A: 

I have used for several years in production Hibernate + JPA. I have never relied on the ORM schema generation though and I think that this is bad practice in general since you're not totally in control of the schema this way and ORM will not always generate the optimal schema. Using something like Liquibase for tracking schema migrations in a much better idea IMO.

Bozhidar Batsov
from a domain-driven perspective, you should not need to to be in control of the schema. In fact, you should not care how exactly is your domain model represented as a relational schema.
Bozho
Bozho, in production environment, you care how your domain model is represented as a relational database. Once you have put your application in production, you can no longer make changes to the database as you wish. In most cases, someone else will take over the database.
josh
@Bozho, theory and practice rarely coexist peacefully. I have seem bugs in ORMs leading to incorrect or non-optimal schema being generated. Also there are projects using ORMs that target only a specific DB and the clients insist on using all db optimizations possible - not pretty, but you know what they say - the customer is always right...
Bozhidar Batsov
Bozo, what you say is philosophy, in real world every part of the system is examined including schema!(This is the same if you say that if you do body building you should care about your mussels and you do not need to care about your stomach)
darko petreski
+1  A: 

Only use auto generated schema's for the early development and prototyping. The generated DDL will almost never satisfy any experienced DBA. Further the database is properly going to live longer than the current application code, so spending time on its design is usually well worth the effort.

When choosing a mapper go for a flexible one and stay clear of the object-obsessed mappers, since these often only supports limited database customization. Hibernates poor stored procedure integration comes to mind, as does JPAs lack of support for custom type mappers.

Object mappers like IBatis and EclipseLink are safe choices as they allows you to map almost anything, ensuring that you can create both a great domain model and a nifty schema design. Also note that Spring JDBC has come a very long way (in particular the very handy SimpleJDBCTemplate), so while technically not a ORM it does allow you to do anything you want without writing tedious JDBC boilerplate code.

Lars Tackmann
Clarify why an ORM framework shouldn't be "object-obsessed" and how any ORM framework allows you to "refactor your database schema".
ireddick
A good ORM should satisfy both DBAs and application developers. I want to have both a great database design and a great object model. Mappers like Hibernate forces the database design to be in a certain way, that I find very limiting.
Lars Tackmann
This accepted answer expresses a very personal point of view and doesn't illustrate the general perception of ORM's added value and when to use them or not. First, ORM are supposed to generate the SQL, supporting stored procedures is just a facility. Still, using stored procedure isn't the philosophy of ORM (just don't use an ORM if you are using stored procedures). Second, comparing Hibernate and iBATIS is like comparing apples and oranges (not even mentioning Spring JDBC), iBATIS isn't an ORM, it's a data mapper. But it is true that ORMs don't fit good with esoteric schemas though.
Pascal Thivent
+2  A: 

ORMs are used very widely in real-world applications. The schema change issue you mentioned is typically dealt with in one of two ways:

  1. As suggested by earlier answers, you can maintain the schema manually in the database and have the ORM update its schema to match what it sees in the database.

  2. You can have the ORM check the database's schema against its own information about the object model and generate the necessary queries to update if there have been any changes.

In my experience, any decent ORM should be able to handle #1 and most seem to be able to manage #2, but it's not quite as universal.

As for which is better, well... That depends a lot on how you view the relationship between your database and your object model.

If your primary interest is on the database and you use an ORM to provide an OO wrapper around the tables and fields to make them accessible more easily, then you'll probably want to go with #1 and keep full control over the database.

If, on the other hand, you view the object model as supreme and are primarily using the database as a dumb persistence mechanism, with the ORM serving to simplify that task, then you'll probably want to use #2 so you can focus on the object model and not have to worry about the underlying database details. Or you might want to take a look at key/value stores, object-graph persistence engines, or other forms of NoSQL databases in order to get better support for straightforward object persistence without having to worry about schema changes on the database side at all.

Dave Sherohman
Thanks Dave. i have a few comments thought. If i change the database as per your option one, i have to update my mapping metadata - annotations or xml. i prefer annotations and that would mean recompilation and redeployment. (change in version, QA process etc). I would love to avoid that.
josh
A: 

I have been using Hibernate for a few years now, and I am very happy with it. I use the schema generator only to create a brand new database, but for upgrades, I use man-made sql scripts.

I don't think it is possible to reliabily automate the upgrade of the schema: if you rename a field for instance, an automatic tool would remove the field and add a new one, thereby losing content.

Maurice Perry