• Post Reply Bookmark Topic Watch Topic
  • New Topic

Where should my customer ID be defined when using Model and DAO?  RSS feed

 
Juan Chris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following `Customer` model:



And the following implementation of my `CustomerDAO`, named `CustomerMySQLDAO`:



My MySQL `Customer` table has the columns `id` (PK auto-increment unique), `name`, `surname`, `cpf` (unique), `birthdate`, `email` (unique). In this case my only `Customer` id is the one generated by the database, I don't have any id set in my model. Should I alter my database so that `id` column isn't auto-generated and I add a line `private static int proxId = 1;` in my model and I set a new id for each instance with `this.id = proxId++;` inside my constructor? Because of this lack of id inside the model, my `update()` needs to be based on `cpf` as I don't have a `customer.getId()`. Or perhaps I should drop `id` entirely and solely base on `cpf` as it's already unique and every Brazilian citizen must have one. What's the right thing to do in this case?

PS: This isn't a real-world software, I'm just training models and DAO pattern.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Customer model should have an id field, and that value comes from the database when the Customer is INSERTed.
Since a new Customer won't have been inserted yet, the id will need to be an Integer or Long object, and not an int or long primitive, as you'll want to be able to set it to null.

When doing an INSERT through JDBC you can get the generated key out, so you can set the Customer id appropriately, if needed.
 
Juan Chris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Your Customer model should have an id field, and that value comes from the database when the Customer is INSERTed.
Since a new Customer won't have been inserted yet, the id will need to be an Integer or Long object, and not an int or long primitive, as you'll want to be able to set it to null.

When doing an INSERT through JDBC you can get the generated key out, so you can set the Customer id appropriately, if needed.


So, using this idea, my Customer will only "exists" after I insert it into my Customer table, right? After a do a simple `Customer bob = new Customer("Bob", "Walts", "15219819245", new Date(), "bob@example.com")`, bob will have `bob.getId()` returning null, I need to insert bob into my DB and during the insert I need to retrieve the generated PK auto-increment id from the table and then I use my `bob.setId(Long id)` to set the id correctly. Is this aligned to what you said?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is "cpf" anyway? Is it a value that would be unique to each Customer? In general, using identifiers that don't have any special meaning allows for more flexibility in your design. Also, ORM frameworks are also pretty good at handling the details of keeping autogenerated identifiers in synch with your program object's ID field when you map them to SQL INSERT statements so I would concur with Dave's advice: add a customerId field to your Customer class.

One caveat you should be aware of in this approach has to do with implementing equals() and hashCode() for your domain objects. See this document: https://developer.jboss.org/wiki/EqualsAndHashCode?_sscc=t

This is why I asked about the "cpf" field. Perhaps you can use it as the "business key" mentioned in that article and use it in your equals() and hashCode() instead of the autogenerated DB customer Id value.
 
Juan Chris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:What exactly is "cpf" anyway? Is it a value that would be unique to each Customer? In general, using identifiers that don't have any special meaning allows for more flexibility in your design. Also, ORM frameworks are also pretty good at handling the details of keeping autogenerated identifiers in synch with your program object's ID field when you map them to SQL INSERT statements so I would concur with Dave's advice: add a customerId field to your Customer class.

One caveat you should be aware of in this approach has to do with implementing equals() and hashCode() for your domain objects. See this document: https://developer.jboss.org/wiki/EqualsAndHashCode?_sscc=t

This is why I asked about the "cpf" field. Perhaps you can use it as the "business key" mentioned in that article and use it in your equals() and hashCode() instead of the autogenerated DB customer Id value.


CPF is like a Social Security number (SSN). It's a set of numbers issued to Brazil citizens.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something unrelated I want to point out about your design: Your Customer constructor has way too many parameters. My rule of thumb is that when a constructor has more than three or four parameters, then switch to using a Builder instead.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juan Chris wrote:CPF is like a Social Security number (SSN). It's a set of numbers issued to Brazil citizens.
In that case, consider using "cpf" as a business key as mentioned and the autogenerated customerId as the object key for your ORM needs.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juan Chris wrote:s this aligned to what you said?


Yes, that's pretty much it.
Obviously, if the cpf is already in there then you'd get an error.
At that point you need to decide what to do with the Customer.
 
Juan Chris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both of you. I believe this cleared my mind regarding this `id`
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!