Hibernate workflow

I am trying to write a program with Hibernate. Now my domain is complete and I am writing a database.

I am confused about what to do. Should I

  • create my sql tables in classes and let hibernate do them
  • Or create tables in a database and reconstruct it and let hibernate do my work?

I heard the first option from someone and read the second option on the Netbeans website.

Does anyone know which approach is right?

+4
source share
4 answers

It depends on how you best conceptualize the program you are writing. When I design my system, I usually think in terms of entities and their relationships with each other, so for me I start with my business objects and then write down my sleep mappings and let hibernate create the database.

Other people may think better in terms of database tables, depending on which approach is right for them. Therefore, you must decide which one works for you based on your experience.

+4
source

I believe that you can do this, so it depends on preferences.

Personally, I write the part by hand. While Hibernate does a reasonable job of creating a database for you, it does not, as I can do myself. I would suggest that this applies to the Java classes that it produces, although I have never used this function.

As for the generated classes (if you sent the class generation route), I put that each field has a getter / setter, whether the fields should be read only or not (someone said that thread safety and variability), and that you can behavior because it is redefined if you regenerate classes.

+1
source

Definitely write java objects, and then add perseverance and let hibernate generate tables.

If you go the other way, you lose the advantage of OOD and all that good.

0
source

I am for writing Java first. This may be a personal preference.

If you analyze your domain, you will probably find that this is some duplication.

  • For example, audit columns (creator and user editor created and edited by time) are often common to most tables.
  • An identifier is often a common field.

Look at your domain to see your duplication.

Duplication is a reusable feature. You can use inheritance or composition. Benefits:

  • less time: you will have much less things to write,
  • logical: the same logical field will be written once (it will be another set of similar fields)
  • reuse: in the client code for your objects you can write reusable code. For example, if all your objects have the same identifier field, called an identifier, because of their superclass, client code can make a general call to object.getIdent () without having to find out the exact class of the object, so it will be used more than once.
0
source

All Articles