mySql + Sql Server + Spring Jpa - mysql

This is more of a Q/A kind of question, i am looking for an example to replicate and learn.
Is there a way to use two different database at different servers from my spring application.
Currently my project uses MySql internally, so i have no issues to bind my JPA repositories or Entities file. It uses the database without any problem.
The problem is that i want to use one entity class that is a table in Sql server (entirely different from the mysql database that i am using currently for all entitiy class).
Please can you provide me example or the steps to be taken towards accomplishing this. I just want to point my Entity class to different database.

There is a whole example there.
You basically create 2 spring configurations, each defining its own DataSource bean.

Related

Creating the MySQL table from the Java Spring boot #entities / classes, is it even possible?

I want to create my Database (MySQL) using my Java Spring boot #entities & classes defined already with all the required fields and associations. Is it possible to do automatically (By a make migration or migrate command mechanism) ? or do i have to explicitly write the SQL queries e.g the Create Table ones.
PS : tools such as flyway arent of a big help as they require to write SQL code despite defined JAVA Code.
Thank you,
Spring Boot Uses Hibernate under the hood as an ORM mapping tool.
Hibernate indeed has such a feature:
In plain hibernate:
hibernate.hbm2ddl.auto is the parameter that you're looking for: use the create as a value
Other values also can come handy, read this SO thread for more information
Spring boot names this parameter slightly different but the logic behind stays the same
Update 1
*Based on Op's comment:
Unfortunately I don't have any access to code that works with the relational databases. But given Spring boot driven application you probably already have src/main/resources/application.properties (or yaml).
So you should: Read the relevant chapter of spring boot configuration
Then,
In the application.properties (or yml) put
spring.jpa.hibernate.ddl-auto=create
And start the application. It should create the tables during the hibernate startup if everything else is configured correctly.
If

Connecting to other database types in the 2sxc module

Is there currently anything in the 2sxc module that allows you to connect to other database types? Specifically I would like to connect to a MySQL database. I know you can connect to other tables within the database.
This depends a bit on your question. Let's split
Can you use Razor-Views in 2sxc to visualize data from any kind of database?
Yes, just use c# code to get the data. Just create the sql-objects or whatever in .net and use that. For example, I wrote about using PetaPoco http://2sxc.org/en/docs/docs/feature/feature/2583
Can you use non-sql-data in your Visual Query Designer
Yes, but you'll have to work a bit. The easiest way is to map your my-sql tables in the DNN SQL server. This is a bit like a translation layer, which then let's use use them as if they were in the SQL Server.
A harder way is to create your own data source. Best to inherit the SqlDataSource (which provides a lot of security around parameter injection etc.) https://github.com/2sic/eav-server/blob/master/ToSic.Eav.DataSources/SqlDataSource.cs - and then modify it to use your mysql

Not possible using NHibernate?

I am new learner to NHibernate. I am trying out samples with MSSQL database. We all know query using MSSQL is different from MYSQL database. If i need to use this sample for MYSQL, do i need to change anything other than configuration settings? Also i need to know is there anything that is not possible because of NHibernate?
There are multiple ways of configuring NHibernate.
What you need to set is called:
The dialect is responsible for converting IQueryable, ICriteria or so called HQL (hibernate query language) to the SQL of the corresponding database.
But if you want to have this general, you must pay attention to the ID (in some cases even use locking). SQL has so called auto-identity, while Oracle works with sequencers. But NHibernate has it's own ways of generating identity.
I would advise you to:
use latest NHibernate (not older than 3.3.x.x)
try out fluent nhibernate mapping
eventually find some example on nhibernate + fluent + unity/structure map (DI framework) - mostly you find something related to DDD projects

grails/gorm/mysql/hibernate

I have a simples question. I have been trying to learn Grails by my own, and i managed to do a simple application using Grails/Gorm.
1 ) Later, i decided to use Mysql instead of Gorm - i just needed to configure the 'DataSource' and download the driver.
2 )So if i want to use hibernate between both (Grails and MYSQL) like this:
http://www.grails.org/doc/latest/guide/15.%20Grails%20and%20Hibernate.html, i need to make an 'hibernate.cfg.xml' file, and specify my mysql database url, user, pw etc .. and i have to map each Class in Grails for MySql columns.
So what is the diference between 1) and 2) ? and what exactly hibernate does. Give examples if possible
PS. Please correct me if i said something wrong, im kinda new to this
I think you are a bit confused here.
GORM is not a database, it is a ORM that maps you Groovy classes to database tables. It uses Hibernate under the covers to achieve this (Hibernate is also an ORM).
The default database Grails uses is an in-memory HSQL DB. If you want to use MySQL instead of that, all you need to do is change the settings in conf/DataSource.groovy.
You don't need to create any Hibernate xml files. That part of the documentation you've linked to is to allow people with existing Hibernate domain models to easily re-use them.
Hope this helps clear things up.
cheers
Lee

what's the best practice to config Hibernate-Spring for multiple database relationship?

We have a main mysql database which all of the applications within our organization need the data within this database. How can I config my java web application to use this database with it's specific database when I use Spring and Hibernate within my application? How can I config JPA annotations for example between CONTRACT table within my database and COUNTRY table within our main database? what's the best practice to do this?
The best practice is not to use multiple databases.
You can have 2 DataSource instances and two SessionFactory / EntityManagerFactory + persistnece unit instances, but that will be a huge mess and you should not do it.
Instead you have two options:
all developers use the same development database server
duplicate the remote data locally