i just complete a small java, spring , hibernate application. now i want put default value in data base . like admin password, pin location like that. i have on .sql file . how to insert it in data base when it load first time.
-
you can use `DEFAULT` clause in SQL – jmj May 02 '11 at 07:16
-
Proposed to be duplicate of http://stackoverflow.com/questions/673802/how-to-import-initial-data-to-database-with-hibernate – Satadru Biswas May 02 '11 at 07:21
2 Answers
Hibernate won't help you there, I think, but you can always resort to Spring JDBC:
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:com/foo/sql/db-schema.sql"/>
<jdbc:script location="classpath:com/foo/sql/db-test-data.sql"/>
</jdbc:initialize-database>
See: 12.9.1 Initializing a database instance using Spring XML

- 292,901
- 67
- 465
- 588
There can be different solutions to do that.For example you can use some xml files(or any other type of file that can have a schema) that hold the start up values and have a utility that will run and insert these values in the database (in the first time you run a setup program) or you can have SQL scripts that have the SQL command that insert these values to the database. But there's another question that I would like to ask and I think that might concern you too. What if you want to update an existing database with some factory values? Do we have to replace the existing data with these new values or we should ignore them and insert only those values that do not exist. If that's the case I suppose that using a tool that manage the logic of this process would be more appropriate.
For the company that I am working with maintaining a running database is more important than creating a new one so instead of having SQL scripts in place we use a custom tool to read some xml files and decides what it should to with its data.

- 5,214
- 34
- 65
-
thanz for your replay... but i am using hibernate in my project. can I use hibernate for database initialization . – jaleel May 02 '11 at 08:41