Database migration from Mysql To SQLServer using hibernate - java

I want to write a script which will transfer daily data from Mysql to SQL server.
I have decided to do this using hibernate.
There are three databases in mysql called db1, db2 & db3. I want to transfer data from all these databases to SQL server. Table structure on SQL server is identical to MYSQL. Simply we can say this script is to take backup of mysql data on sql server.
Now my question is:
Is there any other simple method to do this?
How can I connect to 3 different databases (db1,db2,db3) of mysql as well as sql server using hibernate?
in hibernate.cfg.xml url property takes one database name only:
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db1</property>
So in this way I need to write 6 hibernate.cfg.xml files to connect to mysql as well as sql server.

One of the constructors for Configuration does take a file path:
public Configuration configure(String resource) throws HibernateException {
standardServiceRegistryBuilder.configure( resource );
// todo : still need to have StandardServiceRegistryBuilder handle the "other cfg.xml" elements.
// currently it just reads the config properties
properties.putAll( standardServiceRegistryBuilder.getSettings() );
return this;
}
So... what you'll need to do is copy your hibernate.cfg.xml file as many times as you need and configure different sessions using the standard way.

Related

How to add schema/other user to database url given to java?

I'm trying to connect a spring boot java application to an Oracle database. Oracle SQL Developer shows the tables I wish to query being in the DB named testdb under Other users -> `testUser.
I can connect to the DB using url jdbc:oracle:thin:#localhost:1521:testdb. However, when I use an SQL statement
SELECT * FROM SCHEMA_DEFINITION WHERE SCHEMA_NM = ?
Java doesn't find the table named SCHEMA_DEFINITION. Using testUser.SCHEMA_DEFINITION in the SQL statement does work. How can I tell Java to look for all the tables in Other users->testUser?
I have tried setting the datasource's schema (dataSource.setSchema("testUser");) and changing the url (adding ?search_path=testUser and ?currentSchema=testUser).
None of these work.
it's not a java issue, what you need is to log into the user testUser so you can query those tables without the verbose syntax if you really need to keep these queries as is, and run them from testdb then you need to create synonyms for those tables inside testdb schema:
CREATE SYNONYM TESTDB.SCHEMA_DEFINITION FOR testUser.SCHEMA_DEFINITION;
do this for each table and they will work.
Found the solution in github.com/embulk/embulk-input-jdbc/issues/144. dataSource.setSchema("testUser"); works if I use ojdbc7 (I was using ojdbc6 in my pom.xml).

Derby Embedded Database Create Table Structure

I am trying create a swing application with embedded database using netbeans IDE. First in the netbeans services tab i have created a database and connected using embedded driver. Then i have created all the tables and relationships there.
In the swing application i connected embedded database like this
private static String dbURL = "jdbc:derby:derbysample;create=true;user=root;password=root";
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
//Get a connection
conn = DriverManager.getConnection(dbURL);
This creates the database and i could able to connect. But i doesn't create tables that i created. It's is standalone application. So for different machines it need to create database and tables. There are many solution?
Create programatically tables [Add another java file to create tables and relationship]
Get Schema of Database and build it.
My Question is "is there any easy way to add database structure to embedded database in standalone application?
Abstract of question is:-
Using a derby embedded database with large number of tables and relations. What is the best solution?
Create tables Programaticallly - How can i make it as runs only one
time for each install
Use ant build tools to generate tables
Is there any other ways?
Am assuming what you want is to create a database with specific tables during installation and use the same afterwards.
Derby is light weight database stored in single directory. Having said that creating tables is as simple as executing create table sql queries. Run this while you install.
create=true; in connection string means create an empty database if a matching one is not found (at the given path). This should be done only done at the time of installation.
You mentioned that you created the database with tables. While connecting from application you got an empty database.
Looks the application is not able to access the previously created database at given path (derbysample directory at working directory) and is creating a new database. Try giving the full path without create=true in connection string
private static String dbURL = "jdbc:derby:d:\\somepath\\derbysample;user=root;password=root";
This will force application to use an existing (previously created) database and throw an exception if the same is not found.

Where are my tables in H2?

I deploy my web app to the WildFly 8.02 Final server. I use default out of the box DataSource that comes with the server with jndi name space:
java:jboss/datasources/ExampleDS
and I use default url:
jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
to access H2 from IntelliJ IDEA database client tool or other client applications (I use url connection, because tcp connection via default port doesn't work).
The app is basically an empty WAR with just entity beans and persistence.xml file. I intend to test purely what tables will be created in the underlying data source in accordance to my annotations.
Here is the thing: when I set in persistence.xml:
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
war is succesfully deployed, but when I connect via any client tool to the H2 data source I can see only pre-defined tables after:
SELECT * FROM INFORMATION_SCHEMA.TABLES
I can create tables via client tools and I am able to track their existence by the previous SQL query. So it seems that tables just hadn't been created by the JPA framework.
But when I change persistence.xml to the different schema creation mode:
<property name="javax.persistence.schema-generation.database.action" value="create"/>
Deployment will fail with jdbc exception telling that JPA tries to create already existing tables, but at the same time client tools still do not show any user created tables.
Thx.
Take a look at http://www.h2database.com/html/features.html#in_memory_databases :
To access an in-memory database from another process or from another computer,
you need to start a TCP server in the same process as the in-memory database
was created. The other processes then need to access the database over TCP/IP
or TLS, using a database URL such as: jdbc:h2:tcp://localhost/mem:db1.
Look here for an example:
H2 database in memory mode cannot be accessed by Console

How to identify the script file from where HSQL datatbase is populated?

There is an already existing Java project that extensively uses Hibernate and Spring. And for unit testing it uses an in-memory HSQL data base. I am trying to figure out WHERE this database is getting populated from.
Can someone tell whence I can start looking? Or how to find the file containing the script for creation the tables?
Btw, the tables DO get created. That I confirmed by opening the Database Manager.
If your connection URL begins with jdbc:hsqldb:mem: then this is not populated by HSQLDB but by Hibernate or Spring.
If the connection URL begins with jdbc:hsqldb:file: then the specified file path is the location of the HSQLDB .script file.

Play! JPA: Enforce not to create Entity on load

I am connecting to an external database (i.e. Oracle 10g) using my Play 1.2.4 application.
I have provided all the DB details in the application.conf file, then added the ojdbc.jar in the classpath and the connection seems to work fine. But the problem is everytime when the application access some data for the first time. it is trying to create the entity table (which is already present) and hence throws an exception.
I don't want to create any table since its already there with some data, hence how can I avoid this?
There might be some configuration to set in the application.conf for accessing the external database where the table is already available(I believe), kindly help me on this.
Below is the conf file:
application.mode=dev
%prod.application.mode=prod
db.url=jdbc:oracle:thin:#localhost:1521/orcl
db.driver=oracle.jdbc.OracleDriver
db.user=****
db.pass=****
#%test.module.cobertura=${play.path}/modules/cobertura
%test.application.mode=dev
%test.db.url=jdbc:h2:mem:play;MODE=MYSQL;LOCK_MODE=0
%test.jpa.ddl=create
%test.mail.smtp=mock
Note: Presently I am connecting to the external Oracle database and not with the embedded H2 database.
In your application.conf set the following property to none:
jpa.ddl=none

Categories