Hello,
I have a problem with displaying the h2 tables in browser I've created on Java Web Application ( Spring 4, Hibernate 5, Thymeleaf 3, H2database 1.4.192 , etc.). It has Java-based configuration.
My DataSource:
@Bean(name = "dataSource")
public DataSource getDataSource() {
logger.info("Setting dataSource properties.");
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase embeddedDatabase = builder
.continueOnError(true)
.setType(EmbeddedDatabaseType.H2)
.addScript(CREATE_SCRIPT)
.addScript(INIT_SCRIPT)
.build();
return embeddedDatabase;
}
My 'CREATE_SCRIPT':
CREATE TABLE CLIENTS
(
CLIENT_ID INT PRIMARY KEY AUTO_INCREMENT,
CLIENT_NAME VARCHAR(99) NOT NULL,
AGREEMENT BOOLEAN DEFAULT FALSE
);
CREATE TABLE ITEMS
(
ITEM_ID INT PRIMARY KEY AUTO_INCREMENT,
ITEM_NAME VARCHAR(99) NOT NULL,
PRICE DECIMAL(10,2) NOT NULL
);
CREATE TABLE CLIENTS_ITEMS
(
CLIENT_ID INT ,
ITEM_ID INT NOT NULL,
CONSTRAINT CLIENTS_CLIENT_ID_FK
FOREIGN KEY (CLIENT_ID)
REFERENCES CLIENTS(CLIENT_ID),
CONSTRAINT ITEMS_ITEM_ID_FK
FOREIGN KEY (ITEM_ID)
REFERENCES ITEMS (ITEM_ID)
);
My 'INIT_SCRIPT':
INSERT INTO ITEMS
(ITEM_NAME, PRICE) VALUES ('Book', 5.50);
INSERT INTO ITEMS
(ITEM_NAME, PRICE) VALUES ('Hook', 15.00);
INSERT INTO ITEMS
(ITEM_NAME, PRICE) VALUES ('Nook', 199.9);
INSERT INTO ITEMS
(ITEM_NAME, PRICE) VALUES ('Snook', 1.9);
INSERT INTO ITEMS
(ITEM_NAME, PRICE) VALUES ('Stook', 0.99);
INSERT INTO ITEMS
(ITEM_NAME, PRICE) VALUES ('Mobile Phone', 10);
The tables created 100% cuz I can persist and fetch data.
ноя 10, 2016 11:15:59 AM org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory initDatabase
> INFO: Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
ноя 10, 2016 11:15:59 AM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript
INFO: Executing SQL script from class path resource [create.sql]
ноя 10, 2016 11:15:59 AM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript
INFO: Executed SQL script from class path resource [create.sql] in 479 ms.
ноя 10, 2016 11:15:59 AM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript
INFO: Executing SQL script from class path resource [test.sql]
ноя 10, 2016 11:15:59 AM org.springframework.jdbc.datasource.init.ScriptUtils executeSqlScript
INFO: Executed SQL script from class path resource [test.sql] in 11 ms.
ноя 10, 2016 11:16:09 AM org.springframework.orm.hibernate5.HibernateTransactionManager afterPropertiesSet
INFO: Using DataSource [org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory$EmbeddedDataSourceProxy@162b3d47] of Hibernate SessionFactory for HibernateTransactionManager
But when I open h2-console and connect to this database with current username and password I can find nothing.
It's very strange and I don't know what the problem is.
Help me please.
Thank you much.