enter image description here
I am using a schema.sql and data.sql to insert data in h2 database.
each time I run the app, i need to manually run the insert command in h2 console to insert the data. I have one registartion form link in home page for "candidate". I fill data in the form and the data is added in the database. It is working fine in this case.
But I want to prepopultate the data. so I used
spring.jpa.hibernate.ddl-auto=none, now the data in data.sql is being prepopulated, but when I fill the data in registration form after submitting the data I am getting an error.
could not prepare statement; SQL [select employee0_.email as email1_0_0_, employee0_.first_name as first_na2_0_0_, employee0_.last_name as last_nam3_0_0_, employee0_.password as password4_0_0_, employee0_.role as role5_0_0_ from employee employee0_ where employee0_.email=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
gitlab link:
https://gitlab.com/DipronilDey/Assessment
Please help.
By default, Spring Boot uses SpringPhysicalNamingStrategy, generates "first_name" as column name.
You want "firstName", so you need set physical-naming-strategy explicitly:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
reference:
https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/appendix-application-properties.html#common-application-properties
Related
In my application I am using the following #JoinFormula
#JoinFormula(value="(select s.\"Id\" from \"Student\" s where ash.\"Class_Id\" = id)" )
However, at runtime the generated query is:
(
select
s.cla0_."Id"
from
cla0_."Student" s
where
s.cla0_."Class_Id" = cla0_.id) as formula4_0_,
I need to prevent adding cla0_ table alias to the formula query. How could it be done in hibernate?
I am using it in spring boot, and postgres as database.
I have a spring boot application where I use Spring Data JPA with PostgreSQL as the database. I'm letting hibernate create the schema and that is working perfectly, but when I'm trying to populate the database with the import.sql file, an error shows up.
This is the error:
CommandAcceptanceException: Error executing DDL "insert into users(user_id, username, email, password, created_at)" via JDBC Statement
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at end of input.
This is my import.sql file:
insert into users(user_id, username, email, password, created_at)
values(1, 'alex', 'alex#email.com', 'pword', current_timestamp);
This is my application.properties file:
spring.datasource.url=jdbc:postgresql://localhost:5432/website
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL95Dialect
spring.datasource.username=alexander
spring.datasource.password=
spring.jpa.show-sql=true
# drop n create table again, good for testing, comment this in production
spring.jpa.hibernate.ddl-auto=create-drop
# spring.sql.init.data-locations= classpath:/data.sql
# spring.datasource.initialization-mode=always
# spring.sql.init.mode=always
# spring.jpa.defer-datasource-initialization=true
The commented out part in application.properties are things that I've found in other threads that I've tried but with no change in result.
Every table gets created perfectly, and if I copy the contents of import.sql and inserts it into PostgreSQL via the terminal or pgadmin, then it works perfectly and the user gets inserted with 0 problems. So I really don't get what this "syntax error at end of input." could be.
PS. It's not an issue with the word current_timestamp either, if I swap it with for example ('2020-05-06 08:30') I still get the exact same error.
You have better solution for this problem than moving SQL insert into one line. Just add jpa property in application.properties which allows you to put SQL statements in multiple lines:
jpa:
properties:
hibernate.hbm2ddl.import_files_sql_extractor: org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor
Having SQL insert statement in two (or multiple) rows can improve readability
I try to backup the tables content of a H2 DB.
I run : SCRIPT TO '/opt/data/2019-10-10_tr.sql' TABLE EVENEMENT, PASSAGE, COURSE, LIGNE but the file generated contains some information like :
SET DB_CLOSE_DELAY -1;
;
CREATE USER IF NOT EXISTS SA SALT '7ab09337026fac20' HASH 'c...fa387' ADMIN;
CREATE SEQUENCE PUBLIC.HIBERNATE_SEQUENCE START WITH 5664;
CREATE MEMORY TABLE PUBLIC.COURSE(
...
Which I don't want (that's why I wanted to dump only the tables). I don't want them because when I run RUNSCRIPT FROM '/opt/data/2019-10-10_tr.sql' I have an exception :
CREATE SEQUENCE PUBLIC.HIBERNATE_SEQUENCE START WITH 5664 [90035-197]: org.h2.jdbc.JdbcSQLException: Sequence "HIBERNATE_SEQUENCE" already exists; SQL statement:
CREATE SEQUENCE PUBLIC.HIBERNATE_SEQUENCE START WITH 5664 [90035-197]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
And I have this exception because the database is initialized by ddl : <property name="hibernate.hbm2ddl.auto" value="create-drop" />
I don't wish to change this ; basically by saving only database content and restore it in an existing db : it should work, isn't it ? So the question is what is wrong with my SCRIPT syntax though it doesn't save the tables content only ?
SCRIPT command is not designed to export only the data, it is designed to export the schema with or without the data. Currently there is no built-in command for that purpose.
You can try to add the ˙DROP˙ clause to this command to generate commands for dropping existing tables, but you still may have a problem with sequences and your tables will be redefined, so all changes in autogenerated schema will be lost.
You can filter out all non-INSERT commands from the script with your own code.
You can export the complete script and execute DROP ALL OBJECTS before RUNSCRIPT and overwrite everything with it.
I have configured JDBC connection in my JMeter test plan.
The database settings in JDBC connection are configured correctly.
I am extracting employee id from one of my requests response.(i.e Employee_Id)
By using BSF PostProcessor, i have stored the employee id into a variable called as Emp_ID
I want to insert the extracted employee id into my database.
Database used is Oracle SQL developer, Version 4.0.1.14,Build MAIN-14.48.
Table name is : Employee_Details
Column name is : Employee_id ,Datatype: VARCHAR2
In JDBC request,i have selected "Query Type" as "Update Statement" and entered the following query:
Query 1: INSERT INTO Employee_Details (Employee_id)
VALUES (${Emp_ID})
Query 2: INSERT Employee_id='${Emp_ID}'
Parameter Types is given as VARCHAR2.
On both the execution, JMeter displays error as "Cannot create PoolableConnectionFactory (ORA-00923: FROM keyword not found where expected)"
Please provide your valuable suggestion on this.
I found a workaround for this that avoids having problems:
JDBC Request Query Type needs to be: Update Statement
The query needs to be processed as a block
BEGIN
SQL Statement
END;
My expectation is that you have incorrect validation query:
If you left the "Validation Query" box value default - Select 1 - Oracle may not like it very much. You need to change it to select 1 from dual
References:
DBCP - validationQuery for different Databases
The Real Secret to Building a Database Test Plan With JMeter
ORA-00923: FROM keyword not found where expected tips
There are 3 things as follows:
Check the validation query and keep it blank. Screenshot is attached
When you are executing insert query, keep Query Type as Updated
When you are executing query please add database.tablename and then execute.
i need to create a standalone database app using java and HSQL, the constructor of the app creates the database and 2 tables.I wrote the following:
Connection connection =DriverManager.getConnection(
jdbc:hsqldb:file:D:\\prod \\prod,"SA","");
Statement statement1=connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
statement1.executeQuery(query1);
statement1.close();
Statement statement2=connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
statement2.executeQuery(query2);
statement2.close();
connection.close();
aftre running the application, only the first statement has been executed and one table has been created without any SQL exceptions. any help.............best ergards
You shouldn't use executeQuery(text) for CREATE TABLE and other data definition statements.
Use executeUpdate(text) instead.