Does Hibernate create tables in the database automatically
your hibernate.hbm2ddl.auto setting should be defining that the database is created (options are validate
, create
, update
or create-drop
)
Yes it does in your case because of the below property in your config. This is ok during testing but in production you need to disable this.
<prop key="hibernate.hbm2ddl.auto">create</prop>
For me it wasn't working even with hibernate.hbm2ddl.auto
set to update
. It turned out that the generated creation SQL was invalid, because one of my column names (user
) was an SQL keyword. This failed softly, and it wasn't obvious what was going on until I inspected the logs.
add following property in your hibernate.cfg.xml file
<property name="hibernate.hbm2ddl.auto">update</property>
BTW, in your Entity class, you must define your @Id filed like this:
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@Column(name = "id")
private long id;
if you use the following definition, it maybe not work:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;