Hi,
We are migrating an application from hibernate 3.2.3.GA to hibernate 4.1.1.Final. IN the previous application, TransactionHelper is used to generate the IDs.
I want to reuse that Id generator in Hibernate 4.1.1.Final. But TransactionHelper.java has been deleted from Hibernate 4.1.1.Final.
Can anyone please Help me in this regard? The code snippet is below:
public class IdGenerator implements IIdGenerator {
@Override public Long createIdInCurrentTransaction(final String entityName) { return this.createIdsInCurrentTransaction(entityName, 1)[0]; }
@Override public Long[] createIdsInCurrentTransaction(final String entityName, final int idCount) { Long[] ids = null; try { final Connection connection = this.getSession().connection(); ids = this.getIds(connection, entityName, idCount); } catch (final SQLException e) { throw new InfrastructureException(e); } return ids; }
@Override public Long createIdInNewTransaction( final ISessionProvider sessionProvider, final String entityName) { final TransactionHelper helper = new TransactionHelper() { protected Serializable doWorkInCurrentTransaction( final Connection conn, final String sql) throws SQLException { return IdGenerator.this.getIds(conn, entityName, 1); } };
final Long[] ids = (Long[]) helper .doWorkInNewTransaction((SessionImplementor) sessionProvider .getSession()); return ids[0]; }
@Override public Long peekNextId(final String entityName) { Long id = null; try { final Connection connection = this.getSession(); final IdPeekerDcb dcb = new IdPeekerDcb(entityName); dcb.execute(connection); id = dcb.getNextGeneratedId(); } catch (final SQLException e) { throw new InfrastructureException(e); } return id; }
protected Session getSession() { // return // ServerHelperLocator.getContainerSessionProvider().getSession(); return HibernateUtil.getSessionFactory().getCurrentSession(); }
private Long[] getIds(final Connection conn, final String entityName, final int idCount) throws SQLException { final IdGeneratorDcb dcb = new IdGeneratorDcb(entityName, idCount); dcb.execute(conn); return dcb.getDecoratedIds(); }
}
Thanks in advance.
Giriraj.
|