Uploaded image for project: 'Envers'
  1. Envers
  2. ENVERS-75

org.hibernate.id.IdentifierGenerationException (although Identity is autoincrement)

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Major
    • Resolution: Done
    • Affects Version/s: 1.2.0.GA
    • Fix Version/s: None
    • Environment:

      Neatbeans 6.7.1 Hibernate 3.3.2 Envers 1.2.1 GA

      Description

      When trying to save this entity using em.persist() command I am getting error

      23:53:56,942 ERROR JDBCTransaction:277 - exception calling user Synchronization
      org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): javaapplication8.Product_AUD

      As you can see from the source code, the entity does have an identity autoincrement column.

      I have tested this scenario both with MySQL and Derby. The problem repeats.

      If I remove @Audited annotation the save works fine. So problem seems to be in envers. Because the id of Product_AUD should effectively be set before saving, somehow this doesn't happen.

      See the MySQL table schema below to reproduce the test scenario.

      ==========================================================

      package xx.yy.zz;

      import java.io.Serializable;
      import javax.persistence.Basic;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.NamedQueries;
      import javax.persistence.NamedQuery;
      import javax.persistence.Table;
      import org.hibernate.envers.Audited;

      /**
      *

      • @author Jura
        */
        @Entity
        @Audited
        @Table(name = "PRODUCT", catalog = "", schema = "test")
        @NamedQueries( {@NamedQuery(name = "Product.findAll", query = "SELECT p FROM Product p"), @NamedQuery(name = "Product.findByProductId", query = "SELECT p FROM Product p WHERE p.productId = :productId"), @NamedQuery(name = "Product.findByMarkup", query = "SELECT p FROM Product p WHERE p.markup = :markup"), @NamedQuery(name = "Product.findByAvailable", query = "SELECT p FROM Product p WHERE p.available = :available"), @NamedQuery(name = "Product.findByPurchaseCost", query = "SELECT p FROM Product p WHERE p.purchaseCost = :purchaseCost"), @NamedQuery(name = "Product.findByDescription", query = "SELECT p FROM Product p WHERE p.description = :description"), @NamedQuery(name = "Product.findByQuantityOnHand", query = "SELECT p FROM Product p WHERE p.quantityOnHand = :quantityOnHand"), @NamedQuery(name = "Product.findByManufacturerId", query = "SELECT p FROM Product p WHERE p.manufacturerId = :manufacturerId"), @NamedQuery(name = "Product.findByProductCode", query = "SELECT p FROM Product p WHERE p.productCode = :productCode"), @NamedQuery(name = "Product.findByState", query = "SELECT p FROM Product p WHERE p.state = :state"), @NamedQuery(name = "Product.findByOriginalId", query = "SELECT p FROM Product p WHERE p.originalId = :originalId")}

        )

      public class Product implements Serializable {

      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "PRODUCT_ID", nullable = false)
      private Integer productId;
      @Column(name = "MARKUP")
      private Integer markup;
      @Column(name = "AVAILABLE", length = 255)
      private String available;
      @Column(name = "PURCHASE_COST")
      private Integer purchaseCost;
      @Column(name = "DESCRIPTION", length = 255)
      private String description;
      @Column(name = "QUANTITY_ON_HAND")
      private Integer quantityOnHand;
      @Column(name = "MANUFACTURER_ID")
      private Integer manufacturerId;
      @Column(name = "PRODUCT_CODE", length = 255)
      private String productCode;
      @Column(name = "STATUS")
      private Integer state;
      @Column(name = "ORIGINAL_ID")
      private Integer originalId;

      public Product() {
      }

      public Product(Integer productId)

      { this.productId = productId; }

      public Integer getProductId() { return productId; }

      public void setProductId(Integer productId) { this.productId = productId; }

      public Integer getMarkup()

      { return markup; }

      public void setMarkup(Integer markup)

      { this.markup = markup; }

      public String getAvailable()

      { return available; }

      public void setAvailable(String available)

      { this.available = available; }

      public Integer getPurchaseCost()

      { return purchaseCost; }

      public void setPurchaseCost(Integer purchaseCost)

      { this.purchaseCost = purchaseCost; }

      public String getDescription()

      { return description; }

      public void setDescription(String description)

      { this.description = description; }

      public Integer getQuantityOnHand()

      { return quantityOnHand; }

      public void setQuantityOnHand(Integer quantityOnHand)

      { this.quantityOnHand = quantityOnHand; }

      public Integer getManufacturerId()

      { return manufacturerId; }

      public void setManufacturerId(Integer manufacturerId)

      { this.manufacturerId = manufacturerId; }

      public String getProductCode()

      { return productCode; }

      public void setProductCode(String productCode)

      { this.productCode = productCode; }

      public Integer getState()

      { return state; }

      public void setState(Integer state)

      { this.state = state; }

      public Integer getOriginalId()

      { return originalId; }

      public void setOriginalId(Integer originalId)

      { this.originalId = originalId; }

      }

      ====================================

      DROP TABLE IF EXISTS `test`.`product`;
      CREATE TABLE `test`.`product` (
      `PRODUCT_ID` int(11) NOT NULL AUTO_INCREMENT,
      `AVAILABLE` varchar(255) DEFAULT NULL,
      `DESCRIPTION` varchar(255) DEFAULT NULL,
      `MANUFACTURER_ID` int(11) DEFAULT NULL,
      `MARKUP` int(11) DEFAULT NULL,
      `ORIGINAL_ID` int(11) DEFAULT NULL,
      `PRODUCT_CODE` varchar(255) DEFAULT NULL,
      `PURCHASE_COST` int(11) DEFAULT NULL,
      `QUANTITY_ON_HAND` int(11) DEFAULT NULL,
      `STATUS` int(11) DEFAULT NULL,
      PRIMARY KEY (`PRODUCT_ID`)
      ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
      =====================================================================

      public static void save(Object object) {
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("testPU");
      EntityManager em = emf.createEntityManager();
      em.getTransaction().begin();
      try

      { em.persist(object); em.getTransaction().commit(); }

      catch (Exception e)

      { e.printStackTrace(); em.getTransaction().rollback(); }

      finally

      { em.close(); }

      }

      _____________________________________________

      run:
      00:08:36,646 INFO Version:15 - Hibernate Annotations 3.4.0.GA
      00:08:36,658 INFO Environment:543 - Hibernate 3.3.0.SP1
      00:08:36,662 INFO Environment:576 - hibernate.properties not found
      00:08:36,666 INFO Environment:709 - Bytecode provider name : javassist
      00:08:36,669 INFO Environment:627 - using JDK 1.4 java.sql.Timestamp handling
      00:08:36,718 INFO Version:14 - Hibernate Commons Annotations 3.1.0.GA
      00:08:36,722 INFO Version:16 - Hibernate EntityManager 3.4.0.GA
      00:08:36,957 INFO AnnotationBinder:419 - Binding entity from annotated class: javaapplication8.Product
      00:08:36,964 INFO QueryBinder:64 - Binding Named query: Product.findAll => SELECT p FROM Product p
      00:08:36,965 INFO QueryBinder:64 - Binding Named query: Product.findByProductId => SELECT p FROM Product p WHERE p.productId = :productId
      00:08:36,965 INFO QueryBinder:64 - Binding Named query: Product.findByMarkup => SELECT p FROM Product p WHERE p.markup = :markup
      00:08:36,965 INFO QueryBinder:64 - Binding Named query: Product.findByAvailable => SELECT p FROM Product p WHERE p.available = :available
      00:08:36,966 INFO QueryBinder:64 - Binding Named query: Product.findByPurchaseCost => SELECT p FROM Product p WHERE p.purchaseCost = :purchaseCost
      00:08:36,966 INFO QueryBinder:64 - Binding Named query: Product.findByDescription => SELECT p FROM Product p WHERE p.description = :description
      00:08:36,966 INFO QueryBinder:64 - Binding Named query: Product.findByQuantityOnHand => SELECT p FROM Product p WHERE p.quantityOnHand = :quantityOnHand
      00:08:36,968 INFO QueryBinder:64 - Binding Named query: Product.findByManufacturerId => SELECT p FROM Product p WHERE p.manufacturerId = :manufacturerId
      00:08:36,969 INFO QueryBinder:64 - Binding Named query: Product.findByProductCode => SELECT p FROM Product p WHERE p.productCode = :productCode
      00:08:36,969 INFO QueryBinder:64 - Binding Named query: Product.findByState => SELECT p FROM Product p WHERE p.state = :state
      00:08:36,969 INFO QueryBinder:64 - Binding Named query: Product.findByOriginalId => SELECT p FROM Product p WHERE p.originalId = :originalId
      00:08:36,993 INFO EntityBinder:422 - Bind entity javaapplication8.Product on table PRODUCT
      00:08:37,029 INFO AnnotationConfiguration:369 - Hibernate Validator not found: ignoring
      00:08:37,047 INFO HibernateSearchEventListenerRegister:53 - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
      00:08:37,094 INFO DriverManagerConnectionProvider:64 - Using Hibernate built-in connection pool (not for production use!)
      00:08:37,095 INFO DriverManagerConnectionProvider:65 - Hibernate connection pool size: 20
      00:08:37,095 INFO DriverManagerConnectionProvider:68 - autocommit mode: true
      00:08:37,113 INFO DriverManagerConnectionProvider:103 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/test
      00:08:37,113 INFO DriverManagerConnectionProvider:109 - connection properties:

      {user=root, password=****, autocommit=true, release_mode=auto}

      00:08:37,406 INFO SettingsFactory:116 - RDBMS: MySQL, version: 5.1.30-community
      00:08:37,406 INFO SettingsFactory:117 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} )
      00:08:37,422 INFO Dialect:175 - Using dialect: org.hibernate.dialect.MySQLDialect
      00:08:37,425 INFO TransactionFactoryFactory:62 - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
      00:08:37,426 INFO TransactionManagerLookupFactory:80 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
      00:08:37,426 INFO SettingsFactory:170 - Automatic flush during beforeCompletion(): disabled
      00:08:37,426 INFO SettingsFactory:174 - Automatic session close at end of transaction: disabled
      00:08:37,426 INFO SettingsFactory:181 - JDBC batch size: 15
      00:08:37,426 INFO SettingsFactory:184 - JDBC batch updates for versioned data: disabled
      00:08:37,427 INFO SettingsFactory:189 - Scrollable result sets: enabled
      00:08:37,427 INFO SettingsFactory:197 - JDBC3 getGeneratedKeys(): enabled
      00:08:37,427 INFO SettingsFactory:205 - Connection release mode: auto
      00:08:37,428 INFO SettingsFactory:223 - Default schema: test
      00:08:37,428 INFO SettingsFactory:229 - Maximum outer join fetch depth: 2
      00:08:37,429 INFO SettingsFactory:232 - Default batch fetch size: 1
      00:08:37,429 INFO SettingsFactory:236 - Generate SQL with comments: disabled
      00:08:37,429 INFO SettingsFactory:240 - Order SQL updates by primary key: disabled
      00:08:37,429 INFO SettingsFactory:244 - Order SQL inserts for batching: disabled
      00:08:37,430 INFO SettingsFactory:420 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
      00:08:37,431 INFO ASTQueryTranslatorFactory:47 - Using ASTQueryTranslatorFactory
      00:08:37,432 INFO SettingsFactory:252 - Query language substitutions: {}
      00:08:37,432 INFO SettingsFactory:257 - JPA-QL strict compliance: enabled
      00:08:37,432 INFO SettingsFactory:262 - Second-level cache: enabled
      00:08:37,432 INFO SettingsFactory:266 - Query cache: disabled
      00:08:37,437 INFO SettingsFactory:405 - Cache region factory : org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge
      00:08:37,438 INFO RegionFactoryCacheProviderBridge:61 - Cache provider: org.hibernate.cache.NoCacheProvider
      00:08:37,438 INFO SettingsFactory:276 - Optimize cache for minimal puts: disabled
      00:08:37,438 INFO SettingsFactory:285 - Structured second-level cache entries: disabled
      00:08:37,443 INFO SettingsFactory:314 - Statistics: disabled
      00:08:37,443 INFO SettingsFactory:318 - Deleted entity synthetic identifier rollback: disabled
      00:08:37,444 INFO SettingsFactory:333 - Default entity-mode: pojo
      00:08:37,444 INFO SettingsFactory:337 - Named query checking : enabled
      00:08:37,566 INFO HbmBinder:322 - Mapping class: javaapplication8.Product_AUD -> PRODUCT_AUD
      00:08:37,578 INFO HbmBinder:322 - Mapping class: org.hibernate.envers.DefaultRevisionEntity -> REVINFO
      00:08:37,596 INFO SessionFactoryImpl:187 - building session factory
      00:08:37,756 INFO SessionFactoryObjectFactory:105 - Not binding factory to JNDI, no JNDI name configured
      00:08:37,759 INFO SchemaUpdate:138 - Running hbm2ddl schema update
      00:08:37,759 INFO SchemaUpdate:149 - fetching database metadata
      00:08:37,761 INFO SchemaUpdate:161 - updating schema
      00:08:37,783 INFO TableMetadata:62 - table found: test.revinfo
      00:08:37,783 INFO TableMetadata:63 - columns: [rev, revtstmp]
      00:08:37,783 INFO TableMetadata:65 - foreign keys: []
      00:08:37,783 INFO TableMetadata:66 - indexes: [primary]
      00:08:37,800 INFO TableMetadata:62 - table found: test.product
      00:08:37,801 INFO TableMetadata:63 - columns: [markup, product_id, status, purchase_cost, product_code, original_id, description, quantity_on_hand, manufacturer_id, available]
      00:08:37,801 INFO TableMetadata:65 - foreign keys: []
      00:08:37,801 INFO TableMetadata:66 - indexes: [primary]
      00:08:37,815 INFO TableMetadata:62 - table found: test.product_aud
      00:08:37,815 INFO TableMetadata:63 - columns: [revtype, markup, rev, product_id, status, purchase_cost, product_code, original_id, description, quantity_on_hand, manufacturer_id, available]
      00:08:37,815 INFO TableMetadata:65 - foreign keys: [fk277e8720df74e053]
      00:08:37,815 INFO TableMetadata:66 - indexes: [fk277e8720df74e053, primary]
      00:08:37,826 INFO TableMetadata:62 - table found: test.revinfo
      00:08:37,826 INFO TableMetadata:63 - columns: [rev, revtstmp]
      00:08:37,826 INFO TableMetadata:65 - foreign keys: []
      00:08:37,826 INFO TableMetadata:66 - indexes: [primary]
      00:08:37,827 INFO SchemaUpdate:183 - schema update complete
      00:08:38,056 DEBUG SQL:111 - insert into test.PRODUCT (AVAILABLE, DESCRIPTION, MANUFACTURER_ID, MARKUP, ORIGINAL_ID, PRODUCT_CODE, PURCHASE_COST, QUANTITY_ON_HAND, STATUS) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
      00:08:38,079 DEBUG SQL:111 - insert into test.REVINFO (REVTSTMP) values
      00:08:38,180 ERROR JDBCTransaction:277 - exception calling user Synchronization
      org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): javaapplication8.Product_AUD
      at org.hibernate.id.Assigned.generate(Assigned.java:56)
      at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122)
      at org.hibernate.ejb.event.EJB3SaveEventListener.saveWithGeneratedId(EJB3SaveEventListener.java:43)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
      at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
      at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
      at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)
      at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)
      at org.hibernate.envers.synchronization.work.AddWorkUnit.perform(AddWorkUnit.java:63)
      at org.hibernate.envers.synchronization.AuditSync.executeInSession(AuditSync.java:125)
      at org.hibernate.envers.synchronization.AuditSync.beforeCompletion(AuditSync.java:164)
      at org.hibernate.transaction.JDBCTransaction.notifyLocalSynchsBeforeTransactionCompletion(JDBCTransaction.java:274)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:140)
      at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
      at javaapplication8.Main.save(Main.java:85)
      at javaapplication8.Main.main(Main.java:27)
      00:08:38,183 ERROR JDBCTransaction:155 - JDBC commit failed
      java.sql.SQLException: Can't call commit when autocommit=true
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:930)
      javax.persistence.RollbackException: Error while commiting the transaction
      at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1535)
      at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit(JDBCTransaction.java:170)
      at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:146)
      at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
      at javaapplication8.Main.save(Main.java:85)
      at javaapplication8.Main.main(Main.java:27)
      at javaapplication8.Main.save(Main.java:85)
      at javaapplication8.Main.main(Main.java:27)
      Caused by: org.hibernate.TransactionException: JDBC commit failed
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:161)
      at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
      ... 2 more
      Caused by: java.sql.SQLException: Can't call commit when autocommit=true
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:930)
      at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1535)
      at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit(JDBCTransaction.java:170)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:146)
      ... 3 more
      Exception in thread "main" java.lang.IllegalStateException: Transaction not active
      at org.hibernate.ejb.TransactionImpl.rollback(TransactionImpl.java:82)
      at javaapplication8.Main.save(Main.java:88)
      at javaapplication8.Main.main(Main.java:27)
      Java Result: 1
      BUILD SUCCESSFUL (total time: 2 seconds)

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                bestage henry donnell
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: