Generate xml dynamically in java using data from a table in database - java

Here is the problem that i am currently stuck with for the past few days. And I am looking for guidance / approaches on how to handle.Hints & suggestions welcome.
so here is the problem.The database has a table "group" which has two columns : group_id on parent_group_id.group_id is the primary key for the table .All entries in this table represent groups/sub-groups.If one adds a sub-group from the front end ,then an entry gets inserted in to the group table with an auto-generated group_id which MySQL generates.the parent_group_id corresponds to the group_id of the group on which a sub-group was added.So in essence it's acting like a foreign key to the group_id column.My task cut out here is to generate an XML in java using the data from the group table. So this is where i am stuck.I know it's gonna be a recursive function which needs to be written but cant figure out a way how to dynamically create the nodes and fill the data from the Db at the same time.The end XML needs to be sent as json data to the front end.
A group can have n-sub groups and the hierarchy can go on.For ex- Say Vehicle is root node with group_id =1.It can have cars & bikes as sub-groups.so the parent_group_id will be 1 for car and bike and group id say will be 2& 3 respectively.
P.S: this is the first time i am posting here having had used this site for the past one year.Please let me know if any more info is needed or whether you are able to comprehend my problem.

If you split the task into two, it will be more manageable.
Here are some useful links on querying hierarchical data in relational databases and specifically in MySQL:
What are the options for storing hierarchical data in a relational database?
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
http://www.slideshare.net/billkarwin/models-for-hierarchical-data
http://en.wikipedia.org/wiki/Common_table_expressions#Common_table_expression
As long as you have the query result properly sorted, you will be able to traverse it recursively, building the XML tree step by step.

Was able to solve it by using recursive functions :). Loaded all the data using the entity class and then iterated over the data using recursive functions to build the tree like structure.I didn't try and take the sql way.

Related

Mapping multiple tables(same fields) to single entity - Hibernate

I have Node table with the details of the node. Every month this nodes will be copied to new master and user can anytime change the old nodes value. So, I am planning to create the different table for each copy of the master each month. example, node_jan, node_feb, node_mar etc. I can't put all the node in one table adding month field to differential because node table has more than million records and user can create multiple copy in single month. So if use creates 10 copy in single month then that will be 10 million records in single table which will go bigger and bigger slowing down the fetching and updating data.
I am planning to use Hibernate. So, my question is, is it possible that I create one Node class entity to refer to each table(By passing the table name at run-time).
NOTE :
I am aware that I can use inheritance create give this table name to each inherited entity. But remember, I am creating new table at run time so that approach will not work for me.
Question :
Is it possible to use the same entity to perform CRUD operation in multiple database table?(Providing table name which need to be queried at run-time)
Is there any other approach that I can take to design my database other than creating table at dynamically and does not hurt the performance?
Any help would be appreciated.

Trying to properly Save/Load an Array via MySQL

So I have an array of PlayerNames that are in a specific 'clan' that I need to save, so when I load the server it will loop through all of the entries.
Here is what I currently have
MySQL Table
Not sure what I would put for the 'members' basically what I want is to store UUID's of an array I have. It can come out as a string but I just need them to be able to store like
members: uuid, uuid, uuid, uuid
I understand how to build the connection, ResultSet, and the Statement part, I just don't know how to make MySQL know that I am trying to save these list of members as an array. Any help would be appreciated, I apologize if I did something wrong.
The problem here is in your data structure. It's not Normalized where as RDBMS are designed to store normalized data. All Create/Update/Delete/Retrieve operations become a lot easier if the data is normalized.
To normalize your tables, you need to stop storing member ids in the same column as CSV.
The Gang table should
uuid
title
kills
I assume you already have a members table and it looks something like
uuid
member_name
Then you need a new table called gang_members
gang_id
member_id
And create a unique index on those two columns.
Also see https://stackoverflow.com/a/41215681/267540 , Is storing a delimited list in a database column really that bad? and https://stackoverflow.com/a/41305027/267540

Google BigQuery : Inserting data into temporary table for joining it onto another table

I am currently trying to push some data from Java into BigQuery, in order to be able to make use of it only for the next query and then to get rid of it.
The data consists of 3 columns which exists in the table I want to query. Therefore, by creating a temporary table containing this data, I could make a left join and get the query results I am in need of.
This process will happen on a scheduled basis, having different sets of data.
Can you please tell me if that can be done ?
Many thanks !
Using the jobs.query API, you can specify a destinationTable as part of configuration.query. Does that help? You can control the table expiration time using the tables.update API and setting expirationTime.
Alternatively, you can "inline" the table as part of the query that you want to run using a WITH clause in standard SQL rather than writing to a temporary table.

How to resolve data confliction

I have several databases ,and need exchange data between them. When I export from db A import into db B, Id confliction will happen. I think out two approach, no one satisfy me.
select max(id) then create new id to avoid confliction ,but one column store json structure and contains id too! (history reason). So I need create new Id (primary key) and modify all ids in that json column.
or I can add a batch info for each data import. When I import data, I find out every id in sql and add batch id before them. Such as:
The original db like:
ID COL_JSON
11 {id:11,name:xx ...}
I want to insert a new record :11 ,after insert I add a batch info "1000" before id
now db looks like
ID COL_JSON
11 {id:11,name:xx ...}
100011 {id:100011,name:xx ...}
the next batch will be 1001,1002 1003 ..., so if a new 11 record need to be insert the db looks like
ID COL_JSON
11 {id:11,name:xx ...}
100011 {id:100011,name:xx ...}
100111 {id:100111,name:xx ...}
Although the two approach can resolve conflict, I feel the two approach is stupid. Is there some graceful scheme?
For legacy system, there is no better approach except to align with it. We cannot change it more on legacy system, so your 2th approach seems good. Frankly, tt's not stupid and just the right way to go.
I don't understand exactly what your databases exchange.
If you need data from both databases in both of them you could use something similar to your batch but using characters - A11 and B11 ids.
This way you won't have conflicts even if your database grows a lot.
Edit: You could make also a primary key with two fields: the integer ID with autoincrement and a varchar for the original database name.
When I have a table that should be synchronized (not on the fly), I use this approach :
The main table that will be overwritten has a big autoincrement (ie : autoincrement = 100000)
The secondary table that will be merged to the main one has a normal autoincrement starting to 1.
The only requirement is that you ensure that the main table has a big enough autocrement set up, so the secondary table id will never reach the main table first ID

fetch records from databse and generate tree automatically

I have three tables:
Department :
Designation :
Department Designations :
DesignationID and ReportTo are foreign keys of table designation, which shows that a specific designation report to a specific designation. i.e. Manager(Which is designation) Report to CEO(Which is also a designation )
Now I want to generate a tree, like...
I manually enter the entries to shows above tree . i.e
Now My Problem is that, I am not able to write a function in a way that generate this tree, automatically.
In short, After fetching records from database, I want to generate tree as shown above.
Any Solution ?
NOTE: LEVEL OF TREE IS NOT FIX, THERE MAY BE UNLIMITED LEVEL OF TREE
It's Java, so you can make that tree as deep as it needs to be.
Separate the query from the population of the tree from the UI rendering and you'll have no problem. Query the database, populate the tree and give it to the UI to render.
You'll have issues if you try to mingle one part with another.
You can use a Tree structure like this, and then populate it from the query.
Then, you will have different choices, for example:
1) You convert your tree to JSON and iterate it recursively with jQuery;
2) You iterate it directly in Java (recursively), building the output server-side (let's say a big String containing your HTML). Then you simply inject the result in the page (not that good because of coupling between server and client side)
etc...
Here's an hint.
It's a tree you need to iterate recursively over the tree items till
you reach the leaf level in order to populate it and as mentioned in
answer give by #duffymo you should separate logic of tree population
from your query.
Link if you still are unable to solve
Best luck :-)
i think you could use php for drawing this tree.
example.
write the join query for selecting data to row
<li>
while(!empty($row))
{
echo'<li><ul>$row[designation name]';
while(!empty($next))
{echo'<li><ul>$next[designation name]';
//next sentence must be a select query
}
echo'</ul></li>'
}
echo'</ul><li>'
}
this is not ur answer but a hint that can tell the logic.

Categories