Database and Meta DB table for Spring batch - java

I am very new to Spring Batch. Tried the getting started example from spring doc. using spring-boot-starter-parent(1.5.2.RELEASE). Trying to understand where can I see the data that is inserted using this "Person" table in hsql. and also where can I see the Meta data tables for this example after execution. Please help me to understand this.

By default, Spring Boot uses an embedded database (H2, HSQL, Derby) accordingly to your dependencies.
H2 provides a great web console to view the state of your database. You'll find more informations here : https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-sql-h2-console
If you'd like to stay with HSQL, please have a look at this answer : https://stackoverflow.com/a/8880390/8232755

Related

Is there way to record audit database in spring boot using Spring Data Envers with spring JDBCTemplate?

I'm creating a spring web application that uses the MySQL database with spring JDBCTemplate. The problem is I want to record any changes in data that store in the MySQL database. I couldn't find any solution for Spring Data Envers with JDBCTemplates to record the changes.
What is the best way to record any changes of data of database? or by simply writing a text file on the spring app?
Envers on which Spring Data Envers builds is an add on of Hibernate and uses it's change detection mechanism in order to trigger writing revisions to the database.
JdbcTemplate doesn't have any of that, it just eases the execution of SQL statements by abstracting away repetitive tasks like exception handling or iterating over the ResultSet of queries. JdbcTemplate has no knowledge of what the statement it is executing is actually doing.
As so often you have a couple of options:
put triggers on your database that record changes.
use some database dependent feature like Oracles Change Data Capture
You could create a wrapper of JdbcTemplate which analyses the SQL statement and produces a log entry. This is only feasible when you need only very limited information, like what kind of statement was executed and which table was affected.
If you need more semantic information it is probably best to use an even higher level of your application stack like the controller or service to gather the relevant information and write it to the database. Probably using the JdbcTemplate as well.

spring boot sql database DML and DDL scripts

How i could define some schema and data to be inserted into db for
sql database in spring boot
Also could i do this for embedded databases
For example i am using two databases and i want to populate some data or define some schema and apply to different databases before application starts.
A file named import.sql in the root of the classpath is executed on startup if Hibernate creates the schema from scratch (that is, if the ddl-auto property is set to create or create-drop). This can be useful for demos and for testing if you are careful but is probably not something you want to be on the classpath in production. It is a Hibernate feature (and has nothing to do with Spring).
You can take a look in spring docs

H2 RUNSCRIPT equivalent in HSQL

In H2, it is easy to setup in-mem db for unit testing via RUNSCRIPT command as part of connection url itself (No hibernate & Spring required) to set it up.
h2 sample
jdbc:h2:mem:sample;INIT=RUNSCRIPT FROM 'classpath:scripts/create.sql'\\;RUNSCRIPT FROM 'classpath:scripts/create_2.sql'
I am trying to understand if there is a way to have a similar setup for hsqldb too? No success till now. If it is in documentation, point me to the specific link please.
Constraints:
HSQL should be in-memory only.
No Spring and Hibernate should be used.
Thanks
HSQLDB supports a memory database that is read from file, with no change automatically written to file.
Create the memory database with tables and required initial data, then save it with the SRCIPT 'filename' command.
Then open it as a file readonly database:
jdbc:hsqldb:file:filename;files_readonly=true

Trying to get data from Mysql db (Spring Boot + Vaadin)

I'm trying to get and write data to Mysql Db using Spring Boot & Vaadin. I'm new in Spring and it's too boringly to make simple web project with all of Spring's configurations. So I decided to use Spring Boot. And now my headache is - getting and writing data to Mysql Db. I wrote in application.properties configuration for DB, also "spring.jpa.hibernate.ddl-auto=create" create new table in db (if it's not exists), but no data loaded and no data added to db table. As I see from this doc https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html there is no need to write from beginning some classes to connecting db and getting data...
Please help. https://github.com/AntonKostyukewicz/VaadinCrud - here is my sources of project.
Have you looked at the Vaadin JPA Container add-on? If you are willing to talk to the DB in JPA, that's an excellent option. https://vaadin.com/docs/-/part/framework/jpacontainer/jpacontainer-overview.html

Hibernate database name change gives MySQLSyntaxErrorException: Table doesn't exist

I used to have a database called database and everything was working well using hibernate and its models.
I removed <property name="hibernate.hbm2ddl.auto"> to avoid update or create as it's a production server, we want to do it manually.
We recently switched to database2 and so we switched the hibernate configuration file and all the hibernate XML models.
`<class name="com.api.models.database.MmApplications" table="mm_applications" catalog="database2">`
but it keeps looking for database event if we migrated the database, the models and the connexion.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'database.mm_applications' doesn't exist
Does someone can help me ?
UPDATE ----
Hibernate is connecting to the right database (database2), but there is a prefix as a prefix database. making the queries hitting the database instead of database2, and when I try to force the default_schema my queries become :
`... from database.database2.mm_applications ....`
Any idea?
My database is specified in the hibernate.connection.url property. Have you changed that also ? An example would be: jdbc:mysql://localhost/mydatabase
Also, instead of removing hibernate.hbm2ddl.auto then perhaps you should set its value to validate. That way hibernate will ensure that the datamodel matches the database schema.
I found the problem, It was an other application deployed on the same tomcat server using hibernate as well with another database (database) making a conflict with the new application ...
There is still something weird, by connecting to any database, hibernate will use the specified catalog in the hibernate models and so constructing the request using the catalog.table_name
Hope this help someone someday.

Categories