Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new Hibernate table

Tags:

java

hibernate

So I'm still pretty new to Hibernate, and I'm working on a large-ish application that already has a database with several Hibernate tables. I'm working on a new feature, which includes a new @Entity class, and I need these objects to be stored in a new table. The class is declared like this:

@Entity
@Table(name="DATA_REQUEST")
public class DataRequest {

//Some fields, nothing fancy

}

The DATA_REQUEST table does not exist, nor do I have any data to store in it yet. I started the application up, expecting that it would either create the table or crash because it doesn't exist yet. Neither of these actually happened.

So: do I need to create the table manually (easily done)? Or do I need to go somewhere else to tell Hibernate that I need this table? I've seen the hibernate.cfg.xml file, which looks like a good place to start.

like image 885
andronikus Avatar asked Mar 11 '26 10:03

andronikus


2 Answers

You need to specify "create" for the "hibernate.hbm2ddl.auto" property. Read more details here. This is not recommended in production but only for testing purposes.

like image 163
Aravind Yarram Avatar answered Mar 14 '26 00:03

Aravind Yarram


As for adding a new column to the table

  • As long as it is not a not null column you don't need drop the table or restart your hibernate app

  • If you do want to use the column then you need to map the column in the code/hbm file, so you will have to restart the hibernate app

  • If there is no mapping present as far as hibernate is concerned the column does not exisist, If it is a not null column then underlying data base would reject inserts/updates as hibernate will not include the column in generated sql
like image 40
Prasanna Talakanti Avatar answered Mar 13 '26 23:03

Prasanna Talakanti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!