title: Best way to initialize objects when using a dao
body: <p>I'm building a Tomcat app using MySQL. I'm using a DAO pattern to talk to the database. I'm having trouble with where to initialize fields within the objects. </p>
  
  <p>Mostly to save typing... I am using EclipseLink JPA to Generate model Entities from Tables. I am writing the DAO code by hand. I was writing the model by hand but this seemed easier.</p>
  
  <p>The problem I am having is with writing the object back to the database will all the default nulls. For example, this table contains a bunch of id columns. Not all of them have context for every instance of the object. It seems inefficient to create the object and have to set all the fields to zero in order to be ale to save them to the database. If they have not context, I'd like to just leave them alone. i.e. I only want to set the fields that have context for what I am doing at that moment.</p>
  
  <p>It seems best to use use the constructor in the model class to initialize them. But if I do that, EclipseLink will overwrite them the next time I generate the model.</p>
  
  <p>Having the DAO update method check for nulls and set them to zero seems like a kludge. I suppose I could also use a factory to create and initialize the model class. </p>
  
  <p>But I wonder if I am over thinking this... Any of these solutions would work. But there must be an accepted pattern for this. </p>
  
  <p>How should I approach it?</p>
  
  <p>Thanks</p>
  
  <p>Model is just getters and setters. Contructor is empty.</p>
  
  <p>Excerpts of code are below...</p>
  
  <pre><code>Notice notice = new Notice();
  notice.setEvent("Welcome");
  notice.setUserid(user.getId());
  noticeDao.updateNotice(notice);
  </code></pre>
  
  <p>DAO:</p>
  
  <pre><code>//this seems inefficient
  if(notice.getTravid() == null) notice.setTravid(0L);
  if(notice.getBusid()  == null) notice.setBusid(0L);
  if(notice.getSaveid() == null) notice.setSaveid(0L);
  if(notice.getTargid() == null) notice.setTargid(0L);
  if(notice.getTestmode() == null) notice.setTestmode(false);
  
  String SQLupdate = "UPDATE notices SET "
                  + "userid = ?, "
                  + "travid = ?, "
                  + "busid = ?, "
                  + "saveid = ?, "
                  + "targid = ?, "
                  + "testmode = ?, "
                  + "event = ?, "
                  + "status = ?, "
                  + "error = ?, "
                  + "created = ?, "
                  + "modified = ?, "
                  + "log = ? "
                  + "WHERE id = ?";
  ps = conn.prepareStatement(SQLupdate);          
      ps.setLong(1, notice.getUserid());
      ps.setLong(2, notice.getTravid());
      ps.setLong(3, notice.getBusid());
      ps.setLong(4, notice.getSaveid());
      ps.setLong(5, notice.getTargid());
      ps.setBoolean(6, notice.getTestmode());
      ps.setString( 7, notice.getEvent());
      ps.setString( 8, notice.getStatus());
      ps.setString( 9, notice.getError());
      ps.setObject(10, notice.getCreated());
      ps.setObject(11, notice.getModified());
      ps.setString(12, notice.getLog());
      ps.setLong(  13, notice.getId());
      ps.executeUpdate();
  </code></pre>
  
  <p>DB:</p>
  
  <pre><code>CREATE TABLE `notices` (
    `id` int(20) unsigned NOT NULL AUTO_INCREMENT,
    `userid` int(20) unsigned NOT NULL DEFAULT '0',
    `travid` int(20) unsigned NOT NULL DEFAULT '0',
    `busid` int(20) unsigned NOT NULL DEFAULT '0',
    `saveid` int(20) unsigned NOT NULL DEFAULT '0',
    `targid` int(20) unsigned NOT NULL DEFAULT '0',
    `testmode` tinyint(1) DEFAULT '0',
    `event` varchar(40) DEFAULT NULL,
    `status` varchar(20) DEFAULT 'Pending',
    `error` varchar(255) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `modified` datetime DEFAULT NULL,
    `log` text,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8
  </code></pre>
  
  <p>The database table is like this: </p>
  
  <p>ID </p>
  
  <p>Generically, the code looks like this:</p>