Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normalization vs DeNormalization when using a JSON client with a JAVA/RDBMS stack

I have a Angular JS JSON client interfacing with a JAVA/MySQL stack. As MySQL is an RDBMS, all my data is normalized (first three forms of course).

My question can be illustrated with the following example.

I have an Example object and a User object returned by the server.

Example - [{
userId:1,
...
...
..
},
{
userId:2,
...},
{
userId:3,
...}];

User - [
{
userId - 1,
firstName - "John",
lastName - "Doe",
..
},
{
userId - 2,
firstName - "Jane",
lastName - "Doe",
..
}, {...}...
]

When I look through the Example collection using Angular "example_entry in example" and display the Example collection elements, I only have the userId readily available. But if I want to display the firstName and lastName, I can't do it as it's in a different "User" collection. I have to write a helper Angular controller/service method to get the firstName, lastName from User collection and tie it with Example collection objects.

To avoid this problem, I could De-Normalize the Java Ojbects and send out ready to use JSON like this..

Example - [{
userId:1,
firstName - "John",
lastName - "Doe",
...
...
..
},
{
userId - 2,
firstName - "Jane",
lastName - "Doe",
...},
{
userId:3,
...}];

But is this good/bad/terrible? Because now my Java Domain Objects have firstName, LastName duplicated in the Example Object and also the User Object. Any advise on which route is better?

De-Normalize and have ready to use JSON
Keep Data Normalized to avoid duplication and write converter methods on the Client as needed
like image 374
user6123723 Avatar asked Sep 22 '13 04:09

user6123723


1 Answers

You should denormalize your data when you send it to your UI layer.

If your data is in a database, that is a great place for normalized data. Use a view that abstracts that normalization and query against the view. The view lets you join tables together to get denormalized data from your database.

In the UI layer you want to have clear, simple display - that's when angular shines best. Unless you are displaying very large amounts of data on the screen or sending huge amounts over the net, you'll fare much better by just sending a denormalized record straight from your db. The DB is great at doing joins and other data querying functions - it is built for that. Javascript can do that, but it isn't the best use of it.

like image 69
MattK Avatar answered Sep 21 '22 23:09

MattK