@TableGenerator : how to use

15,999

Solution 1

The value for valueColumnName is mispelled as compared with table specified. Also haven't mentioned which row to refer for fetching key, identified by column value(pkColumnValue).

Below is the sample code & can refer TableGenerator documentation, for further reference.

TableGenerator(name="tg" , table="pk_table", pkColumnName="value" , 
valueColumnName="name" , pkColumnValue = "snuf", allocationSize=10)

Solution 2

It can be misleading to see the value 1 in your @TableGenerator table while 6 records have already been inserted in your @Entity table, but the explanation is quite simple. You've set up your @TableGenerator with an allocationSize=10. What that means is: Hibernate has already pre-allocated IDs from 1 to 9 and once the 9th record is inserted in your @Entity table or you restart your application, the next generated ID will be 10 (pk_table.value * allocationSize). Also, before a row with ID=10 or the next row after application restart is inserted, pk_table.value is incremented by 1, so when this next chunk of 10 is depleted or you restart the application again, ID generation will resume at 20 (2 * 10).

Share:
15,999
Harmeet Singh Taara
Author by

Harmeet Singh Taara

Harmeet starts his career from Java EE development and building applications using Java frameworks. He is the die-hard fan of technology and exploring new approach, frameworks, methodology and more. With some industrial experience, he gains some knowledge about Scala and reactive applications and loves Lightbend technology stack. That's why he joins Knoldus. In knoldus start building application using Java 8 and Scala and other Lightbend technologies like Akka, Play, Lagom and more.

Updated on June 04, 2022

Comments

  • Harmeet Singh Taara
    Harmeet Singh Taara almost 2 years

    i will try to generate the primary keys using table generator. but when i insert the 6 records in my table, the primaryKey table show only one on value. here is the following code

    My Entity class

    package com.generatorvaluetest.domain;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.SequenceGenerator;
    import javax.persistence.TableGenerator;
    
    @Entity
    public class Snufu {
    private int autoId;
    private int identityId;
    private int sequenceId;
    private int tableId;
    private String name;
    public int getAutoId() {
        return autoId;
    }
    public void setAutoId(int autoId) {
        this.autoId = autoId;
    }
    public int getIdentityId() {
        return identityId;
    }
    public void setIdentityId(int identityId) {
        this.identityId = identityId;
    }
    public int getSequenceId() {
        return sequenceId;
    }
    public void setSequenceId(int sequenceId) {
        this.sequenceId = sequenceId;
    }
    
    @Id
    @TableGenerator(name="tg" , table="pk_table", pkColumnName="name" , 
    valueColumnName="vlaue" , allocationSize=10)
    @GeneratedValue(strategy=GenerationType.TABLE , generator="tg")
    public int getTableId() {
        return tableId;
    }
    public void setTableId(int tableId) {
        this.tableId = tableId;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
      }
    

    This is my main class

     package com.generatorvaluetest.main;
    
     import org.hibernate.HibernateException;
     import org.hibernate.Session;
    
     import com.generatorvaluetest.domain.Snufu;
     import com.generatorvaluetest.util.HibernateUtil;
    
     public class GeneratorValueTest {
    public static void main(String[] args) throws HibernateException{
        HibernateUtil.recreateDatabase();
        Session session = HibernateUtil.beginTransaction();
        for(int i = 0 ; i< 5 ; i++){
            Snufu snufu = new Snufu();
            snufu.setName("jimmy"+i);
            session.saveOrUpdate(snufu);
        }
    
        new Thread(new Runnable() {
    
            @Override
            public void run() {
                Session session = HibernateUtil.beginTransaction();
                Snufu snufu = new Snufu();
                snufu.setName("jimmykalra");
                session.saveOrUpdate(snufu);
                HibernateUtil.commitTransaction();
            }
        }).start();
        HibernateUtil.commitTransaction();
    }
      }
    

    in database when i select the values from pk_table the values are

    |name | value|
    |snuf | 1    |
    

    but in snufu tables there are 6 records