2

I am using Hibernate, Maven, and Eclipse (STS build) to build a project. I'm using hbm.xml files to specify my schema. I want to use Hibernate's hbm2java to generate my model classes. I have it working well and generating the kind of code I want.

It runs perfectly from the command line, generating the model code and then building and testing as expected.

However, Eclipse seems unable to handle it. It will periodically "lose its mind" and be unable to resolve very simple imports and classes referenced in my DAO classes, which are hand-coded. The things it can't find are classes like HibernateUtil. Ironically, it appears to not have any trouble finding the model classes.

The unresolved classes are in target/classes/blah-blah folder at the end of the run. So they're apparently getting copied to the right place.

In a "continuous integration" environment, is it best to generate the sources once, commit them to my version control, and then disable code gen? Or is it possible to have the code generated each time, thus ensuring I pick up any database changes without human intervention?

Marvo
  • 17,845
  • 8
  • 50
  • 74

2 Answers2

6

IMHO, entities should be the core of your application, and should be designed, implemented and documented with care. They're supposed to be objects, with methods encapsulating behavior. Having them autogenerated is an absurdity, IMO.

Generating them at the very beginning might be an option to get you started, but once they've been generated, hand-craft them and don't generate them again. Add necessary properties and methods as the schema changes, and refactor existing code.

BTW, I really prefer using annotations for the mapping, because it's less verbose, less error-prone, and all the information is in a single place.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thank you. But I want the "source of truth" to be my database schema. By writing both the schema and model classes by hand, I now have multiple places where that knowledge exists. Do you see any value in having the database schema (or in this case, hbm.xml files) be the sole place where the model is defined? (I guess you do define it all in one place: your Java model classes.) – Marvo Dec 31 '11 at 00:25
  • 2
    We worked with annotations, but found them to be the exact opposite: really ugly to look at, and spread all over the code. With hbm files it feels like the database definition is all in one place. But I'm not settled on that point yet. (I really appreciate your answer, despite my questions.) – Marvo Dec 31 '11 at 00:27
  • The database schema is in the database, or in the SQL scripts used to create the schema, and is also in the hbm files, so I don't see how it changes anything to have the classes generated from the hbm or by hand. If you're concerned about the hbm and the java files not being in sync, then use annotations. You should have unit tests anyway to test that everything works correctly. – JB Nizet Dec 31 '11 at 00:30
  • I'm also generating the schema with hbm2ddl and prepping a test database each time by specifying export. That's working nicely. And good point about the unit tests catching such things. I have those in place already. – Marvo Dec 31 '11 at 00:37
  • 1
    Returning to this question some years later, and four years of experience with Hibernate proves to me, anyway, that it really is the best answer. – Marvo Nov 18 '15 at 19:29
  • @JBNizet yes its a perfect example of the Anemic Domain Model: http://www.martinfowler.com/bliki/AnemicDomainModel.html – Lawrence Tierney Nov 30 '16 at 09:01
0

Try this:

From command line traverse to your project directory where the project's pom.xml is present and run:

mvn eclipse:clean eclipse:eclipse

If it says unable to find plugin eclipse then try:

mvn eclipse:install-plugin

First and then try the command above again.

In this way all the maven and project dependencies will be resolved at eclipse level also.

Let me know if this is not what you were looking for.

Jhonathan
  • 1,611
  • 2
  • 13
  • 24
Tarun
  • 939
  • 15
  • 25