Import database from xml - java

I am using IBM DB2 v9.1 and want to export all database to xml file and them import it back when needed. There are 9 tables in my database.
I am using java and hibernate. What I have done so far is: fetch all data through hibernate and fill POJO objects, then export the objects to xml file. Now for import I need to delete all existing databases first and them import xml file data to the database.
Problem is with primary keys (ids). Once id is deleted from DB2 then data cannot be saved with that id and it will be assigned new id. This disturbs foreign key relation. What is the best possible solution for it?

If you want to export/import data for testing purpose you may want to consider DbUnit http://www.dbunit.org/index.html

Perhaps the MERGE statement can come to your rescue. If there already is a row with the matching id, it will let you update the row. If there is no row with a matching ID, then it will let you insert it.
So then the question might become, do you really need to delete the rows from DB2 when you create the XML files?

What do you mean by all database? all data? or even the DDL?
I think you are exporting all data, and you leave the tables created to be refilled with the exported data.
The problem are the constraints and the generated values. There is a good article about generated values: http://www.ibm.com/developerworks/data/library/techarticle/0205pilaka/0205pilaka2.html
For the referential contraints, the best is to drop/deactivate them before import, then import data, and finally recreate/active referential constraints.
Here, a good stored procedure to enable/disable constraints: http://www.dzone.com/snippets/db2-enabledisable-constraints

After importing file all relations will be mapped to check their inter relation. New objects will be created after mapping relation and they will saved in database with new ID’s as DB2 will not save data on an old deleted ID and it saves it to a new ID.

Related

Retrieve table hierarchie from Hibernate

Now and then I come into the situation that I have to display the table hierarchie of a database for further operations, currently in a data migration project where I have to treat "leaf tables" (tables which are leafes in the table dependency tree) in a different way.
I've always wanted to use Hibernate's meta information to retrieve and display the table dependency tree, but never knew how to approach the problem.
So can anyone give me feedback on whether Hibernate provides an API to do this? I am not asking for a complete solution, the information if there is an API and what it is called is absolutely sufficient.
I want to solve the following questions:
Which tables are in the database?
Is a given table a root table (not dependant from other tables)?
Is a given table a leaf table (dependant from other tables but no table is dependant from the given table)?
Which tables are dependant from the given table?
On which tables does the given table depend?
I know how to retrieve the mapping between entities and tables:
How to discover fully qualified table column from Hibernate MetadataSources , but I want the relationship between the tables.
In a custom MetadataContributor you can access metadataCollector.getDatabase() which exposes the full relational model to you. You just have so save that into a static volatile variable and then access it later on in your app to do whatever you want to do with it.

Is it possible to save Dynamic Tables in SQL server?

I'm working on a school project where I need to make a dynamic table in JavaFX, which should be saved in my SQL database.
The user should be able to specify the number of rows and columns needed.
Is it possible to save a dynamic table in SQL?
I've thought about exporting the dynamic table to a CSV file, and then save the information in the CSV file as a string. Then when you load the table again, a method would be needed to convert from string to table again. Seems like a stupid and inefficient way to do it though.
I've read some places that I should use XML or JSON in some way, but I never understood how.

Mysql data export tool

so, say I have a SQL(Mysql) database containing 4 tables {A, B, C, D}
and I want to create a testing database which contains a subset of data from the first database (both in time and type).
so for example:
"I want to create a new (identical in structure) database containing all of the data for user "bob" for the last two weeks."
the naive approach is to dump two weeks of data from the first database, use vagrant / chef to spin up a new empty database and import the dump data.
however, this does not work as each table has foreign keys with each other.
so, if I have two weeks of data of "A", it might rely on a year old data from "D".
My current solution is to use the data layer of my java application load the data in to memory and then inserted it into the database. however, this is not sustainable / scalable.
so, in a roundabout way, my question is, does anyone know of any tools or tricks to migrate a "complete" set of data from one database to another considering a time period of 1 table and including all other related data from the other tables as well?
any suggestions would be fantastic :)
Try your "naive approach", but SET FOREIGN_KEY_CHECKS=0; first, then run your backup queries, then SET FOREIGN_KEY_CHECKS=1;
There is the way to recreate similar database with part of data, not so simple, but can be used:
Create new database (only schema), for example using buckup or schema comparer tool.
The next step is to copy table data. You could do it with a help of data comparer tool (look at schema comparer link). Select tables you need and check record data to synchronize.

How many tables can be mapped in hibernate mapping.?

I am new to hibernate and am using xml file for mapping. I want to map more than 8 tables, is it possible or not ?
I am using mysql for DB. In all that mapping examples they use maximum 3 tables in mapping. If I change the value in one table that has to reflect it on to next one.
For Example I am giving the Username in my Employee Table and use that one in my login table. If I am mapping 2 tables it means, the username in employee table can automatically update to my login table. Is this correct ?
And how many tables am I able to map in single xml file.
A hibernate component may be your solution. Also hibernate definitions can be in separate files and hibernate will figure it out.
https://www.google.com/search?q=hibernate+components

How to accomplish CRUD operations on dynamic forms?

I have followed Balusc's 1st method to create dynamic form from fields defined in database.
I can get field names and values of posted fields.
But I am confused about how to save values into database.
Should I precreate a table to hold values after creating form and
save values there manually (by forming SQL query manually)?
Should I convert name/value pairs to JSON objects
and save?
Should I create a simple table with id,name,value field and
save name/value pairs here (Like EAV Scheme)?
Or is there any way for persisting posted values into database?
Regards
It look like that you're trying to work bottom-up instead of top-down.
The dynamic form in the linked answer is intented to be reused among all existing tables without the need to manually create separate JSF CRUD forms on "hardcoded" Facelets files for every single table. You should already have a generic model available which contains information about all available columns in the particular DB table (which is Field in the linked answer). This information can be extracted dynamically and generically via JPA metadata information (how to do that in turn depends on the JPA provider used) or just via good 'ol JDBC ResultSetMetaData class once during application's startup.
If you really need to work bottom-up, then it gets trickier. Creating tables/columns during runtime is namely a very bad design (unless you intend to develop some kind of DB management tool like PhpMyAdmin or so, of course). Without the need to create tables/columns runtime, you should basically have 3 tables:
1 table which contains information about which "virtual" DB tables are all available.
1 table which contains information which columns one such "virtual" DB table has.
1 table which contains information which values one such column has.
Then you should link them together by FK relationships.

Categories

Resources