开发者

JPA/Hibernate - Default data

When I have create-drop set as hbm2ddl I want the app to also insert some default data.

Is it possible to somehow write some extension to JPA or Hibernate that can开发者_如何转开发 do it?

default data = 10 inserts! with a very simple data!


During create or create-drop Hibernate by default executes SQL statements from the file named import.sql in the classpath (if exists), so you can place your SQL script there.

Since Hibernate 3.6 you can also specify other files with hibernate.hbm2ddl.import_files property.

See also:

  • Environment.HBM2DDL_IMPORT_FILES


I don't think you can do it with hibernate out of the box, but you can write a small app that does it for you. I have a spring bean that when it's loaded and the DB is empty, it reads a sql file with a bunch of inserts to populate the DB.

So in my case, the same application that I run, is also responsible for populating the DB.


There is a tool called DbUnit which does this in JUnit tests. Not sure if this is what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜