3

I'm writing a simple webapp to show my coding skills to potential employers. It connects with an API and receives a JSON file which is then deserialized using Jackson and displayed in a table form in the browser. I want to enable the user to persist the Java object in a Postgres database using Hibernate. I got it to work and it does the job nicely but I want to make it more efficient.

Whenever there is no data in the JSON response to put in the object's field (right now all the possible JSON attributes are present in the Java class/Hibernate entity in the form of String fields) I put an empty String ('') and then, with all fields having something and no null objects, it is stored in the database.

Should I only store what I have and put no empty strings in the DB (using nulls instead) or is what I'm doing now the right way?

  • If value in json is null, You should store it without modifying it. – Pramod Aug 17 '18 at 11:55
  • This might be more appropriate (and get more useful responses) on https://codereview.stackexchange.com/ as it's more a matter of opinion than a question about how to achieve something. – DaveyDaveDave Aug 17 '18 at 11:58
  • 3
    Not for codereview, there's no code to review. Maybe dba or software engineering. – 404 Aug 17 '18 at 12:07
  • Not entirely unrelated : there are some RDB vendors that do not allow you to distinguish emtpy from NULL : https://stackoverflow.com/questions/203493/why-does-oracle-9i-treat-an-empty-string-as-null (Your question is about postgre, but as you mention the context is employment-related, if I were to interview someone who touches the subject, I would definitely ask "what about other DB vendors ?"). – GPI Aug 17 '18 at 12:15

2 Answers2

0

Null is an absence of a value. An empty string is a value. But that don't impact much to memory. If you want to display data repeatedly and don't want conversion from null to empty string while retrieval you can go for empty string ''.

But if you want unique constraint for values other than empty string '' then use null.

Sometimes null and empty '' can be used to differentiate either data was known or not. for known but not available data use empty and for unknown data null can be used.

0

Use NULLwhen there isn't a known value.

Never use the empty string.

For example, if you have a customer which didn't supply his address don't say his address is '', say it is NULL. NULL unambiguously states "no value".

For database columns that must have a value for your web application to work, create the backing table with NOT NULL data constraints on those columns.

In your unit tests, call NULL, ..._address_is_null_ and test for success or failure (depending on if the test should trigger no errors or trigger an exception).

The use of '' in databases as a sentinel, a special value that means something other that '', is discouraged. That's because we won't know what you meant it to mean. Also, there might be more than one special case, and if you use '' first, then it makes restructuring more difficult to add others (unless you fall into the really bad practice of using even more special strings to enumerate other special cases, like "deleted" and so on).

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138