Announcement Announcement Module
Collapse
No announcement yet.
Loading data into the embedded database. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loading data into the embedded database.

    Hi there.. im trying to load some data on the server startup into my embedded database like this :
    Code:
    <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

  • #2
    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

    Comment


    • #3
      yes, i am using hibernate. For example I have an entity :

      Code:
      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
      Code:
      INSERT INTO UserRole values("ROLE_USER");
      I execute this script with configuration in my spring application context xml

      Code:
      <jdbc:embedded-database id="dataSource" type="H2">
      		<jdbc:script location="classpath:test-data.sql"/>
      	</jdbc:embedded-database>
      '
      Stacktrace :
      Code:
      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(DbException.java:316)
      	at org.h2.message.DbException.get(DbException.java:167)
      	at org.h2.message.DbException.get(DbException.java:144)
      	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:365)
      	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:222)
      	at org.h2.engine.Session.prepareLocal(Session.java:420)
      	at org.h2.engine.Session.prepareCommand(Session.java:381)
      	at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1071)
      	at org.h2.jdbc.JdbcStatement.executeUpdateInternal(JdbcStatement.java:120)
      	at org.h2.jdbc.JdbcStatement.executeUpdate(JdbcStatement.java:109)
      	at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.executeSqlScript(ResourceDatabasePopulator.java:167)
      	at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:120)
      	at org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory.populateDatabase(EmbeddedDatabaseFactory.java:146)

      But the reason is simple. The schema does not exist. If you refer to the manual :
      http://static.springsource.org/sprin...l/ch12s08.html
      it says that first you need a script for schema creation
      Code:
          <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...

      Comment

      Working...
      X