java - How to check if object have been saved to database -


I am using Spring JDBC and I want to make a method that checks whether the database has been continued so far or not, which means that there will be an automatically increased key.

I have a code whose name is id is int since primitive entries are auto-assigned, if no other value is given, then I can make a method That checks for id == 0 but then I have to make sure my primary key starts at sequence 1.

I can also determine if the object has been saved or not, but I did not want to do a query for it.

I think the last option was to change the type of id to integer and then check that it is null but I Do not know that this is a good way to do this.

Is there a way to do this in the Domain Layer? to integer to replace id

A good idea is considered: Frameworks of persistence such as PPAs encourage it (see this example and see through it).

Using an object type immediately gives you an unreserved object to immediately check that id == zero . Just for completeness:

  • id == null : saved object, a INSERT
  • Id! = Null : the object has already been saved, UPDATE

    In this way, you should have 0 Also other integer values) meaning as a special case uninitialized ; You already have null , which processes the idea of ​​a special case properly.

    Adding a Business Key

    In a better approach, it is recommended to use a business key in additon for database-managed IDs. . The business key is an entity identifier, which, unlike the auto-generated primary key ( rental key ), has some meaning for the user ( natural key ).

    You should override the par method to compare two objects using a professional key, and not a auto-generated primary key.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -