Java - country / state / district dropdown in application level - java

I need to implement drop downs for country, state and district.
For getting a list of state, I don't want to make a query to the DB.
i.e. When the server starts up, all the list of countries, states and districts should be loaded on to the memory using static class or methods. From there I should be able to fetch list of states and list of districts. For example, from one district, I should be able to find out from which state and which country it belongs to.
Every country, state and district, will have an id, title, shortcode.
My prime intention is to reduce the number of query request to the DB serve.

Related

Applying filter and paginaton on different micro services

There are two microservices. One for Account service, other for customer service.
Account service stores the data such as account id, customer id, account balance, interest earned and other monetary stuff.
Customer service has personal data such as an customer id, home address, email id, the frequency for sending emails/alerts, Customer-type(Premium, Gold, Silver)... etc.
The account has customer id as the foreign key to map the account and customer data.
The requirement is such that I need to build a search UI memory-efficient. Fetch all the accounts which belong to a particular customer type (premium/gold) and have a balance greater than 200$. So if you see there is cross join filter ie filter needs to be applied on both the microservices to get the desired data. Also, only 10 records would be fetched (page size).On click on the next button, the next 10 records would be fetched.
So how do I fetch the records/store data for this scenario? This is a sort of design /architecture question. I am not expecting the query syntax or sql syntax or hibernate(ORM) syntax.
Should I fetch all the records at the same time, keep it in memory and then apply filters along with pagination. Or Should I use some batching?
Also, I won't prefer SQL joins. This create dependency. If database tables get altered, the query will get impacted

Redistribute/transform qty from one form to other

We are a distribution company where we buy products from supplier /distributor where the products are packed in boxes with a specific pack type and has other attributes associated to them like Branch, Country, Distributor name, etc.
Once these products are in our warehouse, we redistribute or repack them to as per our need and sell them. But if the seller specifies that some of the attributes of the product cannot be changed(We call them as locks), we retain them.
The below image shows issue data from the supplier and the Receive data(Repacked Data) which we do in our warehouse. If the pack type says 1X15 implies that the 1 box contains 15 products. The pack type always starts with 1X[Y] where Y is a variable.
Those attributes that cannot be changed are indicated in Locks column. Notice that the Qty that we get from the supplier and the qty we repack are same. Also, the Qty for the locked attribute products is the same.
We have a screen where the user keys in all the supplier data and the repacked data. But the user will not specify how the issue data is distributed or repacked.
My goal is to find a mapping data of the issue qty with the receive qty keeping the locks intact in a fast and efficient way.
Final Map data which we need to generate:

Java SQL Object Oriented Approach to Tables

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?

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.

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