0

Please bear with me. I am trying to unravel a mystery I've seen in last few days.

I have a following scenario. I have a table named "Bar" with three columns as "A", "B", and "C" in oracle DB. Please refer to figure 1.

I deployed new DDL changing its data structure so that it has columns as "A" and "D" (dropping "B" and "C"). Please refer to figure 2.

Now I have a web application (JSF + Spring Data JPA + Hibernate) that has java entity domain object called "Bar.java" which maps to the table "Bar". However this entity model's fields are not updated yet. It still has "A","B" and "C" fields (columns). If I deployed the application to an app server (e.g. weblogic), would hibernate framework alter the actual table by adding back "A","B" and "C" columns back? Please refer to figure 3.

Figure 1.

+-----------+
| A | B | C |
+-----------+

Figure 2.

+-------+
| A | D |
+-------+

Figure 3.

+---------------+
| A | B | C | D |
+---------------+

In a shop where I am, there are multiple developers are working together and some times their local working branch does not have others' latest commits. I am in the process of investigating why the table "Bar" is keep getting altered. Let's say I verified the table is in figure 2. state yesterday but today morning it is switched to figure 3.

My guess is some of the developers working branch still has old entity model and he/she not aware of it, works on their part of code, deploys the app and the table is altered and exception is thrown in the app server.

Can someone validate this assumption?

[update]

John Bollinger ans sbjavateam were right. I found a xml (not persistence.xml) that contains hibernate.hbm2ddl.auto = update which in turn updates a table.

DaeYoung
  • 1,161
  • 6
  • 27
  • 59
  • Hibernate has a feature that, if you enable it, will update your database schema at deployment time as necessary to support your entity model. For more information, see [hibernate.hbm2ddl.auto possible values and what they do](https://stackoverflow.com/questions/438146/hibernate-hbm2ddl-auto-possible-values-and-what-they-do). – John Bollinger Jun 15 '17 at 17:47
  • @John Bolligner: Thank you for your reply. May I ask if you know where would be (xml file?) the place to set that key/value pair? – DaeYoung Jun 15 '17 at 18:02
  • persistence.xml file – fg78nc Jun 15 '17 at 18:21
  • @fg78nc: I appreciate your reply. Indeed I found the file. – DaeYoung Jun 15 '17 at 18:46
  • I don't thik hibernate.hbm2ddl.auto is enabled in the hibernate config file. Only tag I found is persistence-unit. – DaeYoung Jun 15 '17 at 18:48
  • This parameter can be present i java code. Try tofind hbn2ddl.also if it's shared by somebody can use it in local config and dont put it in repository. – xyz Jun 15 '17 at 19:36
  • This option might bring huge problem in production when somebody deploy changes and drop real column – xyz Jun 15 '17 at 19:42

1 Answers1

2

hibernate has config option hibernate.hbm2ddl.auto with list of possible options :

validate: validate the schema, makes no changes to the database.

update: update the schema.

create: creates the schema, destroying previous data.

create-drop: drop the schema when the SessionFactory is closed explicitly, typically when the application is stopped.

somebody can run appilcation with hibernate.hbm2ddl.auto=update and all columnts(tables) will be added

Community
  • 1
  • 1
xyz
  • 5,228
  • 2
  • 26
  • 35
  • :Thank you for your explanation. I checked persistnece.xml file from the project folder but I didn't see mentioned key/value pair. – DaeYoung Jun 15 '17 at 19:36
  • I accept what John Bollinger and sbjavateam offered as answer even though I didn't see that key/value pair from the xml file, they indeed answered my assumption/question of 'Can hibernate alter the table" by mentioning hibernate.hbm2ddl.auto config option. – DaeYoung Jun 15 '17 at 19:44
  • I found that hibernate.hbm2ddl.auto. It was set to "update". I plan to change it as "validate". – DaeYoung Jun 23 '17 at 14:58