gpt4 book ai didi

java - @TableGenerator : how to use

转载 作者:搜寻专家 更新时间:2023-11-01 02:49:42 26 4
gpt4 key购买 nike

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

我的实体类

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;
}
}

这是我的主课

 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();
}
}

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

|name | value|
|snuf | 1 |

但是在snufu表中有6条记录

最佳答案

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

下面是示例代码&可以引用TableGenerator documentation , 供进一步引用。

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

关于java - @TableGenerator : how to use,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13687898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com