• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java DTO/DAO using an ID placeholder till our db gives us one

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wondering how you handle this in the real world.

Lets say i have a member sign up page, i am passing a Member object to my database for insertion. Because the member has not been inserted into the DB you do not have an ID (my id's are auto generated by MySQL.

How do you guys handle this. Would it be bad form for me to just overload my Customer class constructor to create an ID-less object?





vs




whats the "professional" way to handle this?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you storing and retrieving those objects to and from the database?

You could use Hibernate (or another implementation of JPA = Java Persistence API) and let it handle generating the id's for you.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Christopher

Holding the ID on your Member class is fine as long as you are aware that before the instance is persisted, the ID field might be null. I say might because on creation, the field will be null but if you use the same class to update the instance, the ID field will be (must be) present.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christopher Whu wrote:How do you guys handle this. Would it be bad form for me to just overload my Customer class constructor to create an ID-less object?


It kind of depends on what you need to do with these 'ID-less' Customers. Are you likely to actually do anything with them other than creating rows in the database? Or do they become part of some 'pending' set until credentials have been verified? If that's the case, and you need to persist them, you may want to have some sort of status (active/inactive/pending); alternatively, if you want to hold them in Java until you're ready to make them active, you could create a subclass or a wrapper (PendingCustomer?) that uses something like a UUID as its key.

I have to admit to a slight preference for the status code, although it's arguably not entirely normalized; however, you should then run the INSERT as fast as you possibly can, so that you don't have too many Customer objects "in limbo".

Winston
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic