Java SQL Object Oriented Approach to Tables - java

I have a DatabaseObject in Java which can dynamically retrieve information from a database and store it in key-value pairs (it is just an extended LinkedHashMap).
My DatabaseObject can load, delete, update, and insert values into a table, based on provided columns that match the database table.
I have a "Company" table and a "Category" table in my database, and each company can have multiple categories. I also have a "CompanyCategories" table where each company's category is recorded. These are what the tables look like:
Company
CompanyID, Name
Category
CategoryID, Name
CompanyCategories
CompanyCategoryID, CompanyID, CategoryID
In SQL, I normally will apply a simple join with group concatentation to get a row that looks like this:
CompanyID, Name, Categories
1, Dummy Company, Photographer;Videographer
I am trying to figure out how to best pull this information into a Company object in Java that contains references to another Category object. In my view, I'm not pulling the CategoryID or the CompanyCategoryID, but I will need those if I delete a category from a company, since it's in a separate table.
My question is would it be better to execute several SQL statements back to back? In other words:
SELECT * FROM company WHERE CompanyID=1; SELECT * FROM category WHERE CompanyID=1;
And then in Java combine them into a Company object with a List of Category objects, or should I let the database do the joining and just include a lot of extra information in my view?
SELECT * from company_view
CompanyID, Name, CompanyCategoryIDs, CategoryIDs, Categories
1, Dummy Company, 4;6, 2;3, Photographer;Videographer
My problem with the later is that it's going to quickly become problematic to keep straight, while I'm worried about performance with the former. For a single company, it's not going to be a big deal, but 2000-4000 companies could be loaded at any one time.
I really like the object oriented approach I'm taking to dealing with my database tables, but I'm not sure how best to deal with views.
Which would be more efficient?

Related

How to implement one-to-many relationships with DynamoDB

everyone! I have two tables that I would like to join via DynamoDb, but since the latter is not a relational db, I don't know how to map the link between the two tables.
In particular, I have a Price List table and a Detail List table that contains the details of the first one. How can I implement one-to-many relationship in java using dynamoDB with Spring Boot?
DynamoDB is basically a key-value store. You only every perform a lookup based on a key. That key may be artificial, not just a user id, but maybe "user_id#product#order" but still it will be a key-based lookup. If you want to use DynamoDB you have to store the data in a way that all queries that you will need will all boil down to basic key-based access (plus some sorting).
You have to do the exact opposite of normalizing your data and splitting relations into multiple tables: you have to de-normalize all your data to store the data and all the relations just in one table, multiple times, with multiple complex artificial keys. See e.g. https://www.youtube.com/watch?v=HaEPXoXVf2k on how to use LSIs, GSIs, how to model your data, how to choose artificial keys, etc.
That means you will not have Item, Order and OrderItem table that you join together, but you will have just one Everything table which may have the fields: userid, username, ordernumber, itemid, itemprice, itemquantity, itemname, orderdate, shippingaddress, etc.
And if you have three items in an order you will have three entries in this table. That means the username will be in the table very often, that means the itemname will be in the table very often and changing them will be difficult but that is how things are if you want to use dynamodb.
That is how you model one-to-many relations, by packing them into a single table and add proper indexes.
If you do have no idea about the current or future access patterns of your data or how to structure your data properly then dynamodb is the wrong tool for you.
The question you are asking gets at the very essence of working with DynamoDB and NoSQL data modeling. It is not as simple as applying your relational database knowledge to DynamoDB. Take a moment to familiarize yourself with the DynamoDB basics before you get too far into solving this problem.
Watch this video about modeling one-to-many relationships in DynamoDB. I would recommend you watch the entire video from the beginning, as it's one of the best introductions to the topic currently available.

mySQL Java Database Modeling

In trying to create a SQL database that can be queried within java, but I'm not sure how to structure it. Here are my categories:
1. State
2. Cites within each state
3. Venues within each city
4. Specifics about each venue
This might seem simple to some, but I am concerned with the amount of information that will be located in category #4, since that category alone could house the most data. The most important part of this is that I would of course need to query this information in category #4 and return the information. Is it common practice to have a category with a lot of information in it or do I need to break down category #4 even further? Also, another issue I am coming across is that I am using java to create arrays or arrayLists for all this info and I don't know how to get the data from the arrays to mysql. Any info is appreciated.
Creating relational tables for a relational database and querying relational tables in a relational database using Java are two different tasks.
First, let's create the relational tables.
State
-----
State ID
State Name
...
In the State table, you store all the information about a state. The State ID is the primary (clustering) key, and is an auto-incrementing integer.
City
----
City ID
State ID
City Name
...
In the City table, you store all the information about a city. The State ID is a foreign key to the State table. In the few cases where a city is in multiple states, you create a city row for each state. As an example, Bristol, Virginia and Bristol, Tennessee is a city in two states.
Venue
-----
Venue ID
City ID
Venue Name
Venue Type
Venue Address
...
Venue Type
----------
Venue Type ID
Venue Type Description
In the Venue table, you store all the information about a venue. The City ID is a foreign key to the city. You can get the state information from the city row.
In the Venue Type table, you store the various venue types, like hotel, theater, restaurant, retail store, etc.
The only reason you would create a Specifics table is if there are multiple types of specifics for a venue. Otherwise, you can add a Venue Description column to the Venue table.
First, get your relations for your relational tables correct. Then, you can see how to map the relations to Java classes. Classes consisting of lists are probably the correct approach.
Get your code to work with the most direct mapping between your domain objects and your tables. If down the line you realize you're storing too much data in one table, you'll be able to refactor by breaking that data more. But I wouldn't worry too much about having too much data in a table, that's what databases are made for. What you need to think about is how you're going to access that data, and make sure you have proper indexes and primary keys on it.
To insert several rows of data from Java to mysql, search the Web. Java: Insert multiple rows into MySQL with PreparedStatement may be a good start.

Spring JDBC and keeping objects and database in correct state

I am creating an application in Spring JDBC and I have some questions regarding how to keep the object graphs and the database in correct state. If you have a car object with a list of parts. If you remove a part from the car then you need to save this into the database using a update operation in the car repository. Do you then have to find all the parts belonging to the car, then remove whatever part from the parts table? It means that I need to do find operations before updates?
I suppose it depends on the design of your database.
Given cars and parts, I would have something like
table cars: car_id, name
table parts: part_id, description
table installed_parts: car_id, part_id
A Cars object would have a List of Parts, which are drawn from the information in installed_parts.
When car.removePart(part) is invoked, the installed_part where car_id == Car.car_id and part_id = Part.part_id is removed.

Best approach to our database design

We have a database for an e-commerce application, we are working with JPA 2.0.
We have these three tables: products(id, code, name, description, price); soldproducts(id, quantity, product_id); orders(id, date, status, comment).
Here is our problem: when we delete a product from products table we have a constraint problem in the soldproducts, because the product_id column references a product. We want to delete the product, but we want to have access to the products details.
We want to add an aditional column to the products table, something like availability, which tells us if the product is available or not. With this approach, if we delete a product from the application and the product is not referred from the soldproducts, we are just updating this extra column to unavailable, we don't delete it. Otherwise, we delete the product from the table. But we are not sure if this is the best approach.
What do you think, what is the best design approach for this given situation?
The best design approach, in my opinion is to not delete the product row but to mark it as deleted. You will need to add a deleted or active boolean column to your table.
This is what's usually done in this situation on most systems I saw/worked on. Of course, you will need to filter your products query by active (not deleted) products every time. But it's the only way to not loose the historic data references.

java web application - model design for displaying list with a sql join

I am authoring a javabean and would like to know if it should include properties to set values obtained from a sql join?
Say I have database tables for Products and Orders. These are also my java bean names.
Now I want to display a list of all products but in addition to all the product properties, I want to have columns to display last purchase date and last purchased by
My db query to get the product list would need to do joins to gather the additional info. It does not seem correct to have setters and getters for `last purchase date and last purchased by' in Product.java model. I may want to have a 3rd column so constantly adding new columns to my bean doesn't make sense.
How do you go about this? I seem to encounter this when needing to display lists of models in my view.
Have a Map in Product. Upon firing the sql, store hte results of the join as key value pair in the Map. K=lastpurchasedby(columnName) and V= the value
So no need to add individual attributes. They all come in key-value pairs.

Categories