How to pass SQL queries in json file? - java

I'm creating dynamic application with spring boot. I want to get SQL queries from user to make changes without modifications of code. So I want to get it from json property file.
Is this a good approach ? Or there are alternative ways to do the same.

Related

Create schema and tables on demand at the runtime

So as the title suggests - I need to create an application (preferably Spring Boot), which will create schemas and tables based on user input. Basically, a rest endpoint will be offered to the clients where they would upload their data model in json format. I'll be parsing the json and constructing the db artifacts (schema and tables) in runtime. And once all the tables are created, provide a rest endpoint (with unique identifier), to the client, to perform CRUD operations on their schema.
The approach I am considering currently is -
Create a super user in db , before deploying the app which will have priviliges to create new schemas and db
Create prepared statements to invoke schema/table creation on demand. The prepared statements will have place holders to take the schema name and table definition.
After proper authentication, allow users to upload their data model definition in json.
Clean the json and invoke the schema/table creation prepared statements.
Few questions that I had in mind -
Since all these DB operations will be invoked from a single super user's account, is it safe ?
The schemas and tables will be realized using native SQL queries instead of Hibernate's ORM capabilities. Is it safe/efficient ?
For the CRUD operations, is it possible to switch the db connection from super user to the client specific schema created in the earlier steps ? Or should I continue using the same super user for the CRUD operations?
It would be nice if it is possible to switch schemas in runtime using Hibernate/Spring-Boot.
What I would like is a general approach to this problem. I do not need any code.
A typical web application already has permissions to DELETE all the data for all the users.
JPA makes your queries slower, not faster. JPA can help with caching, but it doesn’t seem you need this.
Yes, you can have multiple datasources in Spring Boot. Look at this for example: https://www.baeldung.com/spring-abstract-routing-data-source
Be aware that your database might not like having millions of tables. Query planning, maintanance jobs, backups, etc all get performance penalties. Basically, databases are not designed for your use case.

How to create a temporary database with postgresql initdb?

How do I create a temporary database for PostgreSQL using initdb? Also, how do I populate it with dummy data and how do I delete it after usage?
I have an entire schema of the database. I don't want to create temporary tables one by one. Using initdb, I would like to be able to give the schema and get a database with the tables, etcetera created.
This is for running integration tests of a spring boot application. I use mybatis to access the database.
Can I use any other tool like arquillian?
There's a workaround this, just change the data directory to a RAM file system, like this once you restart your computer, the data is gone. better you won't change anything in your scripts.

Can I use JPQL to query a MySQL database which was populated by loading CSV files?

This is something of a noob question, so please bear with me.
I'm building a Java web app which is deployed on JBoss. Part of the functionality is populating a MySQL DB with data from an Excel spreadsheet. This can be achieved in 2 ways:
Using JExcel / Apache POI to parse the spreadsheet data and creating Entity "beans" which are then persisted to the DB.
Using scripts to convert the spreadsheet to csv files and then load the csv files into the DB.
My question is: If I choose the scripting / csv route, can I still use JPQL to query the DB or will I have to resort to native SQL queries in the Java code?
JPQL can be used to query table independently from method that was used to populate table. Data stored to table is not aware of with which method it was inserted.
JPA is not notified about changes made to data via script, but in typical use case with no additional caches and transaction-scoped PersistenceContext that is not the issue, because query will hit the database and deliver fresh data.

One web app accessing different databases with the same structure

I created a java web application using Spring Roo as the persistence layer and MySQL as the database.
I'll have several customers using that application but it has to be one database per customer. I mean, the same database structure for everyone but having one database(schema) per customer. So how to do that using the current technologies in my application?
I was thinking of something like a URL parameter indicating what schema to use, for example:
Customer 1 should use: http://www.myapp.com/?schema=dbcustomer1
Customer 2 should use: http://www.myapp.com/?schema=dbcustomer2
So now I'm wondering how to pass that schema param value to the Spring Roo's database connection at runtime. Currently it's hard coded in the database.properties file generated by Roo?
Please, also let me know if you think there is a better approach to achieve that.
Thank you,
Gyo
You can customize your Spring Roo application just the way you approach multi-tenancy in a traditional Spring based application.

Switching between embedded Databases in Java with JPA

Im currently working my way towards JPA 2.0 and I start of liking how easy it is to maintain persistent data.
What I'm currently trying to accomplish is using JPA in a basic desktop application. The application should allow me to open embedded databases which are on my file system. I chose H2 databases for now, but I can really live switching to JavaDB or anything else.
What Im trying to accomplish is, that one can open the database file without previously define a persistence-unit in the persistence.xml file.
I can easily define a unit and persist objects, but it needs to be configured first.
I want to write some sort of database browser which allows opening without preconfiguration and recompiling.
http://www.objectdb.com/java/jpa/start/connection
I saw that ObjectDB allows access for this type of PersistenceFactory creation, but I was not able to transfer this example to other databases.
Am I totally wrong with the way I approach this probblem? Is JPA not designed with on-the-fly database access?
Thank you for your help,
Johannes
Not part of the JPA standard. Some implementations may offer their own API to do it. For example with DataNucleus if you go to this page http://www.datanucleus.org/products/accessplatform_3_0/jpa/persistence_unit.html at the end you can create dynamic persistence-units (and hence EMFs), and that implementation obviously allows persistence to the widest range of datastores you'll get anywhere
You can pass a Map of properties to createEntityManagerFactory() call that defines the database connection info, etc. The property names are the same as in the persistence.xml. I assume most JPA providers support this, EclipseLink does.
You will still need to define the set of classes for the database and map them.
If you do not have any classes either, than you could look into EclipseLink's dynamic support,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Dynamic
If you want to make a database browser accessing different databases, you can't use a PU/Entity Manager (imo).
You'll need a dialogue asking a user for the IP/Port of the database, the username/password, the database name to access, and the type of database.
Then all you need to do is create a socket, send requests over the socket, and parse the response into a view.
Since both the request and the response are database specific, the user has to select the proper database driver.

Categories

Resources