0

Unfortunately the subject of the question seems a bit vague, but I don't know anything better. So, I am making a project using Hibernate, PostGreSQL and Spring. As per the functionality the database can be divided into 3 parts. Considering a simple product based app, as Users(U), Products(P), Messages(M). I have a single database, where I have made tables and defined relationships.

As a user might have one-to-many products, the 2 tables are connected within the database. Now consider a situation, when the user comes for registering on the webpage for the 1st time, He/She enters the information to be registered(Firstname, lastname, email,etc). When I am trying to save this with hibernate, will it create an instance of all the tables connected to the user class and try to save the information(leaving info in Products as null) or something else? If yes, then how I can I separate it within database/program logic.

I hope I made the question atleast a bit clear. Thank you.

We are Borg
  • 5,117
  • 17
  • 102
  • 225

1 Answers1

1

You can tell Hibernate to create the tables for you but this doesn't happen by magic or automatic. It usually happens when Hibernate is making the first connection to the database (usually during the config phase when Hibernate checks whether the database matches the entities which you defined).

So creating the tables happens before you can start to load/persist objects if you enable this feature.

Alternatively, you can use SchemaExport to create DDL files as explained here, for example: Generate an SQL DB creation script with Hibernate 4

That way, you can use admin tools to create the tables and Hibernate will use what is already there (and fail with errors when the database schema doesn't match the entities in your code).

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Actually I am implementing a database model in PostgreSQL. So I am wondering how the 3 Users, Products, Messages must be connected within the database so I can just use for E.g only User table at one point without effecting other tables. – We are Borg Sep 10 '14 at 09:15
  • You can configure Hibernate to use the existing schema. Then you must make sure that your entities are written correctly and that you have all the annotations to tell Hibernate the names which you have chosen for tables and columns. Lastly, you need entities for all 3 tables, even if you never change products or messages from Hibernate. Every field that want Hibernate to save to the database must either be a primitive or an entity or you must write a custom converter which tells Hibernate how to load/save this value to the database. – Aaron Digulla Sep 10 '14 at 09:20
  • There is a problem though: When another process changes the database, you must tell Hibernate to flush it's internal caches (or disable caching but that's usually too expensive). Otherwise, Hibernate will not be able to see all changes. – Aaron Digulla Sep 10 '14 at 09:21
  • Yes. I plan to use an existing schema. All the 3 tables have entities. Take a relation here, A user can have one or more Products. I am just wondering how to define these relationships using Hibernate. – We are Borg Sep 10 '14 at 09:31
  • For N:M relations, you need a mapping table. If you don't know what that is, then look at the docs for Hibernate or here: http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html – Aaron Digulla Sep 10 '14 at 09:38
  • Perfect. Just one last question. So depending upon my schema I can use annotations like JoinTable, JoinColumn, etc, correct? And for normalization where I am using foreign keys I will find something else. – We are Borg Sep 10 '14 at 09:42