PDA

View Full Version : Loading data into the embedded database.



bogumillaska
Jul 21st, 2011, 08:11 AM
Hi there.. im trying to load some data on the server startup into my embedded database like this :


<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:test-data.sql" />
</jdbc:embedded-database>


but i get a "table not found" error. It apears that the schema based on annotations is build after the scripts.. So I could fix that adding a second script which create the schema but i would like to avoid that..

Any advice appreciated :)
Regards

dr_pompeii
Jul 21st, 2011, 11:08 PM
Hello

1) About

but i get a "table not found" error.
Post the complete error stack trace

2) could you post the content of your test-data.sql script?

3)

It apears that the schema based on annotations is build after the scripts.
I am assuming you are working with Hibernate or JPA, bear in mind there is a property to let Hibernate create and drop the schema when you startup and shutdown your application

bogumillaska
Jul 22nd, 2011, 04:34 AM
yes, i am using hibernate. For example I have an entity :



package org.elbi.inz.domain;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class UserRole {

@Id
private String roleName;

public String getRoleName() {
return roleName;
}

public void setRoleName(String roleName) {
this.roleName = roleName;
}


}


I try to run a very simple sql

INSERT INTO UserRole values("ROLE_USER");

I execute this script with configuration in my spring application context xml



<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
'

Stacktrace :

Caused by: org.h2.jdbc.JdbcSQLException: Tablela "USERROLE" nie istnieje
Table "USERROLE" not found; SQL statement:
INSERT INTO UserRole values("ROLE_USER") [42102-134]
at org.h2.message.DbException.getJdbcSQLException(DbE xception.java:316)
at org.h2.message.DbException.get(DbException.java:16 7)
at org.h2.message.DbException.get(DbException.java:14 4)
at org.h2.command.Parser.readTableOrView(Parser.java: 4352)
at org.h2.command.Parser.readTableOrView(Parser.java: 4332)
at org.h2.command.Parser.parseInsert(Parser.java:908)
at org.h2.command.Parser.parsePrepared(Parser.java:36 5)
at org.h2.command.Parser.parse(Parser.java:278)
at org.h2.command.Parser.parse(Parser.java:250)
at org.h2.command.Parser.prepareCommand(Parser.java:2 22)
at org.h2.engine.Session.prepareLocal(Session.java:42 0)
at org.h2.engine.Session.prepareCommand(Session.java: 381)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConn ection.java:1071)
at org.h2.jdbc.JdbcStatement.executeUpdateInternal(Jd bcStatement.java:120)
at org.h2.jdbc.JdbcStatement.executeUpdate(JdbcStatem ent.java:109)
at org.springframework.jdbc.datasource.init.ResourceD atabasePopulator.executeSqlScript(ResourceDatabase Populator.java:167)
at org.springframework.jdbc.datasource.init.ResourceD atabasePopulator.populate(ResourceDatabasePopulato r.java:120)
at org.springframework.jdbc.datasource.embedded.Embed dedDatabaseFactory.populateDatabase(EmbeddedDataba seFactory.java:146)


But the reason is simple. The schema does not exist. If you refer to the manual :
http://static.springsource.org/spring/docs/3.0.0.M4/spring-framework-reference/html/ch12s08.html
it says that first you need a script for schema creation

<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>

But i create my schema basing on annotations.. and not with the script.. So this attempt fails.. becouse the scripts are executed before the annotations are read and transformed into schema...

So.. i guess what i need is a programatical execution of the script on startup.. but after the schema is created...