Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Database Correct Approach

Tags:

java

json

I have built a small JavaFX with 2 scenes. The user can input 3 fields (text) and upload some documents for an object.

So I was thinking when the user clicks save a json object is created and appended to a list of Json objects. Those Json objects are then written into a file.

This is of what I was thinking:

{
  "objects": {
       "object1": {
               "field1": "foo"
               "field2": "foo"
               "field3": "foo"
               "folderwithfileslocation": "C:/ProgramFiles/myapp/foobar/" 
               },
       "object2": {
               "field1": "foobar"
               "field2": "foobar"
               "field3": "foobar"
               "folderwithfileslocation": "C:/ProgramFiles/myapp/barbar/" 
               },
.......
....
..
}

These will be read into objects on startup, so the user has access to them, so he can edit them, add, delete, etc etc typical CRUD.

Is this a correct approach ? There would be maximum 500-600 records. Should I add a unique ID (idk like randomUUID())?

Thanks

like image 769
Trt Trt Avatar asked Oct 20 '22 07:10

Trt Trt


1 Answers

Most database schemas would add a UUID as a primary key, but it's important to realise why - the primary key allows you to look up a specific document. If a user updates or deletes a specific document, you'll need a way to find that record in the database so you can apply that action.

If one of the other columns (possibly the folder?) is unique (and not nullable) this would serve as a perfectly good PK itself without adding a UUID. Equally, if the objects are always read as a list you could use the index in that list as a PK, but that makes some assumptions about the ordering of the list. If those won't do the job for you, adding a PK is probably a good idea, and a UUID is one very good way to do that.

Personally, I'd expect it to be worth adding a UUID for debugging alone. The overheads are tiny and it makes a big difference to traceability.

like image 143
hugh Avatar answered Oct 27 '22 00:10

hugh