How Insert In multiple database MongoDB with Java Springboot in Maven project - java

I am wondering if is it possible to insert in multiple databases in MongoDB. I have on my pom.xml project this connection structure:
<configuration.database.host>XXX.XXX.XXX.XXX</configuration.database.host>
<configuration.database.port>27017</configuration.database.port>
<configuration.database.username>useradmin</configuration.database.username>
<configuration.database.password>userpass</configuration.database.password>
<configuration.database>database1</configuration.database>
<configuration.database.collection.documentone>master</configuration.database.collection.documentone>
<configuration.database.collection.documentone.documenttwo>master_advice</configuration.database.collection.documentone.documenttwo>
Now, I need to add in the project another database to insert in antoher document some information, I don't know if I can add another connection and how can I do. for example, in the pom adding:
<configuration.database>database2</configuration.database>
<configuration.database.collection.documentone>cluster</configuration.database.collection.documentone>
of course, considering the configuration in the application.properties file.

Related

How to set properties value in multi-module project?

I have a multi-module Spring Boot Gradle project. I have properties in each module yml file that point to database: user, pass, url.
It's working solution, but it's difficult to change project database. Every time I want switch database user or url, I must change 10+ yml files.
How to avoid this?
You could bind the properties in a class (see here: https://www.baeldung.com/configuration-properties-in-spring-boot) and inject the class where needed.

Java Classes Structure

I am working to a project made in Maven with Spring and i want to start the structure fine from the beginning.
I need help from you to know if is ok what i did until now. So, in have the structure:
src/main/java/com/fabbydesign/controller
src/main/java/com/fabbydesign/model
src/main/java/com/fabbydesign/util
and i have another file in:
src/main/webapp/WEB-INF/appConfigs.xml
in this file i store the configurations of the project, like database login, etc
In the directory utils, i will store utils class for the project. for example, i will make a class that will read the appConfigs.xml to get configurations stored there
Is ok what i did until now?
In the model directory, i have to store the beans for the database, or i can add other types there? Like, the bean for the xml with configs?
Thanks!

Automatic generation of migration SQL for Flyway

Is it possible for new Flyway migrations to be generated by JPA/Hibernate's automatic schema generation when a new model / field etc. are added via Java code.
It would be useful to capture the auto-generated SQL and save it directly to a new Flyway migration, for review / editing / committing to a project repository.
Thank you in advance for any assistance or enlightenment you can offer.
If your IDE of choice is IntelliJ IDEA, I'd recommend using the JPA Buddy plugin to do this. It can generate Flyway migrations by comparing your Java model to the target DB.
You can use it to keep your evolving model and your SQL scripts in sync.
Also, it can create the init script if your DB is empty.
Once you have it installed and have Flyway as your Maven/Gradle dependency, you can generate a migration like this:
Flyway doesn't have built-in support for diff, I use liquidbase within a maven spring boot project and changelogs can be created from JPA/hibernate changes by using:
mvn liquibase:diff
All of the options for liquibase diff are located here:
http://www.liquibase.org/documentation/maven/maven_diff.html
If you want to generate the update SQL automatically, you can ask Hibernate to do so; just add the lines below to your Spring Boot configuration:
spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql
When you execute the application, this will generate a file named update.sql on the root of your project. Now, you can just copy and paste them into your Flyway migration.
This was adapted from this other answer: https://stackoverflow.com/a/36966419/679240 ; it is basically the same logic, except that one wants to generate a database creation script, while I needed an update script, instead.
BTW, if you want to replace the names of the foreign keys on the script with more readable ones, you could use this regex: ^(alter table .*?)(\w+)(\s+add constraint )\w+( foreign key \()(.*?)(\).*) with this replacement: $1$2$3fk_$2__$5$4$5$6; this will change the names of the FKs in the script to fk_name_of_the_table__name_of_the_field.

How do I use templating with Hibernate and Gradle?

I have a server that talks to a database that I need to test. I connect to the database using Hibernate and manage the dependencies using Gradle. I want to use separate tables in MySql for production and testing. So I have currently this line in hibernate.cfg.xml:
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/production_database</property>
But what I really want is for it to be something like:
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/${DATABASE_NAME}</property>
and then when I run gradle test, DATABASE_NAME can be set to "test_database_name", and when I run gradle jettyRun it'll still be "production_database". This seems like something that should be possible, but when I google for "hibernate teplating" I get references this other thing called HibernateTemplate that has nothing to do with what I want as far as I can tell. What's the syntax that'll make this happen for me?
You should move that property out of hibernate.cfg.xml, and into a database.properties file.
And, Then you can use gradle to modify this file depending upon the argument.
Please refer to Gradle Tasks for this.
ant.propertyfile(
file: "database.properties") {
entry( key: "connectionurl", value: "somevalue")
}

H2 Embedded Database loses tables / data on deployment

So I have an application, and it uses a database, now I need to use an embedded database inside the application so that all you do with it is install the program, and the database is already there (So the client does not have to install a copy of mysql server, wamp, xampp, etc to run the mysql database) with all its information.
I have gotten the application to export into a jar file, which then is made into an exe file perfectly fine, and works on my pc, but when I copy it to another pc and run it, the database is there, but the table data and the tables I put in are gone. What am I doing wrong?
Thanks in advance
There are multiple options. One option is:
Create a SQL script file from the current database using the SQL statement SCRIPT TO '~/script.sql'
Copy the file script.sql from your user home directory to the source directory of your application, so that you can bundle it as a resource. Use the root directory of the source code of the application (because that's the simplest solution).
In the target computer, use the database URL jdbc:h2:~/data;INIT=runscript from 'classpath:script.sql'. This will open the database and run the script. It will read the script as a resource.
There are other options as well, but they require that you read the documentation of the H2 database.
Looking at a similar problem (using an exported database into H2), I found the answer above useful but did not manage to make SCRIPT TO work.
My database is mySQL.
The way to export thet database as a script that worked for me was from linux shell :
mysqldump -u[user] [databasename] > exportscriptfile.sql
I had then to do some polishing work on the script so that H2 can read it (see similar stuff on this SO thread)
Here is my working java based spring configurations using jdbc EmbeddedDatabaseBuilder. The datasource created as a bean here can be used in any jdbc calls :
#Configuration
public class H2DBConfig {
#Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:exportscriptfile.sql")
.build();
}

Categories