# Database dependent ID generation going from hibernate 2 to 3

**URL:** <https://discourse.hibernate.org/t/database-dependent-id-generation-going-from-hibernate-2-to-3/2109>\
**Category:** Hibernate ORM\
**Created:** [January 21, 2019, 10:19pm UTC](https://discourse.hibernate.org/t/database-dependent-id-generation-going-from-hibernate-2-to-3/2109 "2019-01-21T22:19:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmcclung](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@jmcclung](https://discourse.hibernate.org/u/jmcclung)\
**Post date:** [January 21, 2019, 10:19pm UTC](https://discourse.hibernate.org/t/database-dependent-id-generation-going-from-hibernate-2-to-3/2109/1 "2019-01-21T22:19:04Z")

</div>

I am tasked with upgrading the hibernate on one of our java apps. It currently uses hibernate 2 and I am first trying to get to 3. I am new to hibernate and am having trouble reworking the id generator. The app has a db2 database & the OIG.V\_UNIQUE\_KEY table has 1 column & 1 record that changes with each added record to the database.

Here is the current code:

```auto
public class UniqueKeyGenerator implements IdentifierGenerator, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public UniqueKeyGenerator() {
		super();
	}

	public Serializable generate(SessionImplementor session, Object object)
		throws SQLException, HibernateException {
		
		Long assignedid = (Long)((Entity)object).getAssignedId();
		if (assignedid != null)
			return assignedid;
			
		PreparedStatement st = session.getBatcher().prepareStatement("select UNIQUE_KEY_ID from OIG.V_UNIQUE_KEY");
		try {
			ResultSet rs = st.executeQuery();
			final Number result;
			try {
				rs.next();
				//result = IdentifierGeneratorFactory.get(rs, Long.class);
				result = (Number) IdentifierGeneratorFactory.get(rs, new LongType(), session, object);
				
			}
			finally {
				rs.close();
			}
			return result;
		}
		catch (SQLException sqle) {
			JDBCExceptionReporter.logExceptions(sqle);
			throw sqle;
		}
		finally {
			session.getBatcher().closeStatement(st);
		}
	}

}

```

The get method that we are using is now deprecated and I have been unable to find a workable solution. I have searches some new id generator strategies, but have not been able to find one that I could get working. Any help would be greatly appreciated. Thanks

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 22, 2019, 10:04am UTC](https://discourse.hibernate.org/t/database-dependent-id-generation-going-from-hibernate-2-to-3/2109/2 "2019-01-22T10:04:56Z")

</div>

If you need an identity generator that combines the assigned and the IDENTITY one, check out this one:

```auto
public class AssignedIdentityGenerator
    extends IdentityGenerator {
 
    @Override
    public Serializable generate(SessionImplementor session,
        Object obj) {
        if(obj instanceof Identifiable) {
            Identifiable identifiable = (Identifiable) obj;
            Serializable id = identifiable.getId();
            if(id != null) {
                return id;
            }
        }
        return super.generate(session, obj);
    }
}

```

This one works with Hibernate 5, which is probably what you should migrate to.

For more details, check out [this article](https://vladmihalcea.com/how-to-combine-the-hibernate-assigned-generator-with-a-sequence-or-an-identity-column/).
