Question: I have an situation where I need to save a hibernate object, but I am not sure whether an ID would be assigned by calling app (using special logic with in a range - unique) or not. 

If the ID is not assigned, I need hibernate to generate an ID higher than the possible ID range which the app would input (i know the range). Else should go with what using app inputs.

I am working on MySQL - checking to see if I can custom generator like below

```
public class MyDOIdGenerator extends IdentityGenerator{

    @Override
    public Serializable generate(SessionImplementor session, Object obj) throws HibernateException {
        if ((((MyDO) obj).getId()) == null) {
            Serializable id = super.generate(session, obj) ;
            return id;
        } else {
            return ((MyDO) obj).getId();

        }
    }
}

```

But my problem is, I dont know how the super.generate would behave in a clustered environment. Would it maintain the ID synchronization across servers? How do I specify the number to start from? (Because I need to exclude the app using id range when having hibernate generate it)

Please help

Thanks

Answer: 
Actually you will still need to do some stuff with your Models in your controller. Use Repository Pattern do to so, which is pretty similar of querying your models in your controller, but being less verbose:

```
public function serve($company)
{
    return View::make('layouts.singlereview')->withReview(with(new Review)->getCompanyData($company));
}

```

And put the whole logic in your repository:

```
class Review extends Eloquent {

     public function getCompanyData($company)
     {
         return static::select('head', 'body', 'logo', 'name')
                     ->where('company', '=', $company)
                     ->firstOrFail();
     }

}

```
Also, while you are on the subject of creating good code, you might as well check out Eloquent relationships. They may require database restructuring for certain occurrences, but most of the time you should be good. 

With the code you have provided, I can assume that review and company are in a one-to-one relationship, so once this is defined, you can simply retrieve a company object, then do, 

```
$review = $company->review;

```