tags:

views:

32

answers:

2

What are the benefits of Hibernate over just generated DAL based on db tables and sprocs?

i.e. writing or using a tool to generate code for the data access layer versus using hibernate

+1  A: 
  1. hibernate entities are regular POJO's that can encapsulate business logic, and if the database schema changes, the entities can be adapted, they don't need to be re-generated.
  2. the code that takes the entities and persists them (hibernate-core) is widely used and has support in many different frameworks.
  3. additional features like transaction management, a java builder based query language and many other features are already included in hibernate, if those features are something you find useful, then you will have them out of the box

The biggest disadvantage would be that you lose fine-grained control, so look into how well Hibernate does all the things you will need it to do before getting involved... also, it takes quite awhile to learn hibernate and it's many quirks, you will not start using hibernate and be a master of it in the same week... I've been using it for over a year and still have issues that are difficult to find occasionally

walnutmon
+1  A: 

I use Hibernate and reverse-engineer the models and DAOs from ant. The ant tasks generate them by looking at the database. It works very well. As the project continues, it's pretty common for database tables to change which breaks the Hibernate models. We just re-reverse-engineer the models and we're good to go.

Hibernate is easy if you Do It Their Way and stick to common SQL constructs. I just completed 2 reasonably large projects using Hibernate. Hibernate caused no difficulties. If you have to do some fancy query it's easy enough to use SQL though an experienced Hibernater probably wouldn't need to.

What's also nice is you gain the ability to crawl through the classes in a very natural way.

For example, if a Survey has many Questions and a Question has one Answer, you can say

Survey survey = SurveyDAO.get(entityManager, 1L); // 1 is the PK
List<Question> questions = survey.getQuestions();
for(Question question: questions) {
    Answer answer = question.getAnswer();
    System.out.println("Question "+ a.getQuestionNumber()+" = "+answer.getText());
}

And the relationships go the other way too. If we have a Question we can get the survey:

Survey s = someQuestion.getSurvey();
List<Questions> q = s.getQuestions();

So if know a question, it's easy to get all the questions in a survey.

To do all that we didn't have to write any Hibernate code to other than SurveyDAO.get(). Pretty easy really.

Tony Ennis