-->
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.  [ 2 posts ] 
Author Message
 Post subject: hibernate 3.5.x update and IdentifierGeneratorFactory.class
PostPosted: Tue Jul 06, 2010 5:43 pm 
Newbie

Joined: Tue Jul 06, 2010 5:26 pm
Posts: 1
I am trying to update to hibernate 3.5.3 with spring 3.0.2

One issue I am having is that the IdentifierGeneratorFactory.class has changed in the hibernate-core jar. Looks as if the the class was moved into a factory package. The getGeneratedIdentity() method isn't there anymore. We were using IdentifierGeneratorFactory.getGeneratedIdentity() is retrieve the id generated after the record update

Code:
public class TriggerAssignedIdentityGenerator extends AbstractPostInsertGenerator {

    public InsertGeneratedIdentifierDelegate getInsertGeneratedIdentifierDelegate(
            final PostInsertIdentityPersister persister, final Dialect dialect,
            final boolean isGetGeneratedKeysEnabled)
    throws HibernateException {
        return new Delegate(persister, dialect);
    }

    public static class Delegate extends AbstractReturningDelegate {
        private final Dialect dialect;

        private final String[] keyColumns;

        public Delegate(final PostInsertIdentityPersister persister,
                        final Dialect dialect) {
            super(persister);
            this.dialect = dialect;
            this.keyColumns = getPersister().getRootTableKeyColumnNames();
            if (keyColumns.length > 1) {
                throw new HibernateException(
                        "trigger assigned identity generator cannot be used with multi-column keys");
            }
        }

        public IdentifierGeneratingInsert prepareIdentifierGeneratingInsert() {
            return new NoCommentsInsert(dialect);
        }

        protected PreparedStatement prepare(final String insertSQL,
                final SessionImplementor session) throws SQLException {
            return session.getBatcher().prepareStatement(insertSQL, keyColumns);
        }

        protected Serializable executeAndExtract(final PreparedStatement insert)
            throws SQLException {
            insert.executeUpdate();
            return IdentifierGeneratorFactory.getGeneratedIdentity(insert.getGeneratedKeys(), getPersister()
                    .getIdentifierType());
        }
    }



Now that hibernate 3.5.x has dropped the getGeneratedIdentity() from the IdentifierGeneratorFactory.class, how can I retrieve the id generatored keys after the update?


Top
 Profile  
 
 Post subject: Re: hibernate 3.5.x update and IdentifierGeneratorFactory.class
PostPosted: Wed Nov 03, 2010 12:17 pm 
Newbie

Joined: Wed Nov 03, 2010 11:57 am
Posts: 1
You can try this....

Code:
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.AbstractPostInsertGenerator;
import org.hibernate.id.IdentifierGeneratorHelper;
import org.hibernate.id.PostInsertIdentityPersister;
import org.hibernate.id.SequenceIdentityGenerator.NoCommentsInsert;
import org.hibernate.id.insert.AbstractReturningDelegate;
import org.hibernate.id.insert.IdentifierGeneratingInsert;
import org.hibernate.id.insert.InsertGeneratedIdentifierDelegate;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class TriggerAssignedIdentityGenerator extends AbstractPostInsertGenerator {
  public InsertGeneratedIdentifierDelegate getInsertGeneratedIdentifierDelegate(PostInsertIdentityPersister persister, Dialect dialect, boolean isGetGeneratedKeysEnabled) throws HibernateException {
    return new Delegate(persister, dialect);
  }

  public static class Delegate extends AbstractReturningDelegate {
    private final Dialect dialect;

    private final String[] keyColumns;

    public Delegate(PostInsertIdentityPersister persister, Dialect dialect) {
      super(persister);
      this.dialect = dialect;
      this.keyColumns = getPersister().getRootTableKeyColumnNames();
      if (keyColumns.length > 1) {
        throw new HibernateException("trigger assigned identity generator cannot be used with multi-column keys");
      }
    }

    public IdentifierGeneratingInsert prepareIdentifierGeneratingInsert() {
      NoCommentsInsert insert = new NoCommentsInsert(dialect);
      return insert;
    }

    protected PreparedStatement prepare(String insertSQL, SessionImplementor session) throws SQLException {
      return session.getBatcher().prepareStatement(insertSQL, keyColumns);
    }

    protected Serializable executeAndExtract(PreparedStatement insert) throws SQLException {
      insert.executeUpdate();
      return IdentifierGeneratorHelper.getGeneratedIdentity(insert.getGeneratedKeys(), getPersister().getIdentifierType());
    }
  }
}


-------- Gigi


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

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.