java @TableGenerator :如何使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13687898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:37:13  来源:igfitidea点击:

@TableGenerator : how to use

javahibernateannotations

提问by Harmeet Singh Taara

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

我将尝试使用表生成器生成主键。但是当我在我的表中插入 6 条记录时,primaryKey 表只显示一个值。这是以下代码

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

在数据库中,当我从 pk_table 中选择值时,这些值是

|name | value|
|snuf | 1    |

but in snufu tables there are 6 records

但在 snufu 表中有 6 条记录

回答by Nayan Wadekar

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

valueColumnName与指定的表相比,for 的值拼写错误。也没有提到要引用哪一行来获取由列 value( pkColumnValue)标识的键。

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

下面是示例代码 & 可以参考TableGenerator文档,以供进一步参考。

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

回答by Rares Oltean

It can be misleading to see the value 1 in your @TableGeneratortable while 6 records have already been inserted in your @Entitytable, but the explanation is quite simple. You've set up your @TableGeneratorwith 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 @Entitytable or you restart your application, the next generated ID will be 10 (pk_table.value * allocationSize). Also, before a row with ID=10or the next row after application restart is inserted, pk_table.valueis 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).

@TableGenerator表中已插入 6 条记录时,在表中看到值 1 可能会产生误导@Entity,但解释非常简单。你已经设置了@TableGenerator一个allocationSize=10. 这意味着:Hibernate 已经预先分配了从 1 到 9 的 ID,一旦第 9 条记录插入到您的@Entity表中或您重新启动应用程序,下一个生成的 ID 将是 10 ( pk_table.value * allocationSize)。此外,在ID=10插入应用程序重新启动后的一行或下一行之前,pk_table.value增加 1,因此当下一个 10 块耗尽或您再次重新启动应用程序时,ID 生成将在 20 (2 * 10) 处恢复。

回答by Tom

put this code above to your primary key column definition

将此代码放在上面的主键列定义中

@TableGenerator(
            name="empGen", 
            table="ID_GEN", 
            pkColumnName="GEN_KEY", 
            valueColumnName="GEN_VALUE", 
            pkColumnValue="EMP_ID", 
            allocationSize=1)


    @Id
    @GeneratedValue(strategy=TABLE, generator="empGen")
    int id;

where you can give any value to recognize a particular table in pkColumnValue field.

您可以在其中提供任何值来识别 pkColumnValue 字段中的特定表。

for further information on this refer the documentation of @TableGenerator from below ink http://docs.oracle.com/javaee/6/api/javax/persistence/TableGenerator.html

有关这方面的更多信息,请参阅下面的 @TableGenerator 文档 http://docs.oracle.com/javaee/6/api/javax/persistence/TableGenerator.html