-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Best Practices on object refreshing
PostPosted: Thu Feb 03, 2011 11:02 pm 
Newbie

Joined: Thu Feb 03, 2011 10:29 pm
Posts: 4
TL;DR I want to prevent the user from overwriting objects in the database, yet allow them submit custom objects.
Hello Everyone,
I am pretty much a noob with Hibernate and Spring. But I have an architectural problem that I was wondering if you could help me. Here is the scenario, the user wants to save a car object. The car object contains various parts. Let's start with the wheel, assume the wheel has 2 properties "wheelId" and "wheelManufacturer"
Code:
Wheel
    private String wheelId;
    private String wheelManufacturer;
    //getters and setters.


Now when the user sends in the car for saving , the wheel can either be one that exists in the database, or a custom wheel. If it is one that exists in the db, I want to use that one, basically preventing the user from overwriting the stored wheel. If it doesn't exists, I want to write it to the db. Here is how I do it now.
Code:
class CarService
....
   public Car saveCar( Car car )
   {
     ....
       Wheel frontWheel = car.getFrontWheel();
       //Does the front wheel exists
       if( frontWheel != null )
      {
          //Get the id of the wheel
          String frontWheelId = frontWheel.getWheelId();
          if( frontWheelId != null )
          {
              //the wheel has an id, so it should exist in the db, get that copy
               Wheel dbWheel = wheelDao.getById( frontWheelId );
                if( dbWheel != null )
                {
                     //we found the db copy of the wheel, set that as the property of the car
                     car.setFrontWheel( dbWheel );
                }
          }
       ....
      }
   }


Needless to say, this becomes redundant when you consider all the different parts a car can have. I was wondering about how other people solve this problem.

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.