0

when we want to start a project with hibernate and a Database (MySQL or Postgresql for example) in java it's better to perform the relationships between tables in database or in hibernate or both ? i want to know what experts do about this ?

thanx

Hossein Boka
  • 277
  • 2
  • 7
  • 15
  • Not sure what you mean; could you be more specific? – Dave Newton Oct 25 '11 at 09:16
  • 1
    I guess the question is - should he define database-level foreign-key/constrains or just use what hibernate offer. If thats the case i'd suggest to stay away from database-level FKs, cause it can lead to a mess in no time. Hope that helps – SirVaulterScoff Oct 25 '11 at 09:37

2 Answers2

0

The whole point of hibernate, imho, is to remove yourself from the database and sql, and any dependcy to the database (and its vendor). It is literally just a store of data, and can be swapped out at a later date.

So you define your relationships (and all business logic) in the java code and let hibernate create the tables for you.

You should be aware of the relevant hibernate setting to enable table creations, and its pitfalls.

Community
  • 1
  • 1
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
0

You can keep the relationships in your entities.

You can use the maven plugin (http://mojo.codehaus.org/maven-hibernate3/hibernate3-maven-plugin/hbm2ddl-mojo.html) to generate the relevant db schema.

This step also helps you validate the relationships that you define in the entities.

Scorpion
  • 3,938
  • 24
  • 37