@TableGenerator可以保存表中最后使用的ID而不是下一个可用的ID吗? [英] Can @TableGenerator save the last used id in the table instead of the next available?

查看:286
本文介绍了@TableGenerator可以保存表中最后使用的ID而不是下一个可用的ID吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要连接到供应商数据库并插入客户数据。用于生成新客户ID的序列表存储上次使用的ID(不是下一个可用的ID)。我无法在jpa或hibernate文档中找到任何可以指示hibernate将seq表中的id视为最后一次使用而不是下一个可用(使用@TableGenerator时)的方法。



是否需要编写一个自定义生成器,其行为与@TableGenerator基本相同,唯一的区别是序列表中值的处理方式是什么?



我的客户实体定义如下:

  @Entity 
public class Customer {

@Id
@TableGenerator(name =cust_gen,table =SEQUENCE,pkColumnName =target,
pkColumnValue =customer,valueColumnName =id,allocationSize = 1)
@GeneratedValue(strategy = GenerationType.TABLE,generator =pat_gen)
public long getCustomer_id(){
return customer_id;
}

public void setCustomer_id(Long id){
this.customer_id = id;
}

...

}

谢谢!

解决方案

我有同样的问题。以这种方式修复它:
使用Hibernate org.hibernate.annotations.GenericGenerator而不是像这样持久化TableGenerator:

  import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name =name)
public class Name implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.TABLE,generator =names)
@GenericGenerator(name =names,strategy =com.ourpackage.SybaseTableGenerator ,参数= {
@Parameter(name =table_name,value =new_key_numbers),
@Parameter(name =value_column_name,value =key_number),
@参数(name =segment_column_name,value =name),
@Parameter(name =segment_value,value =names_key)})
@Column(name =names_id)
私人长ID;

并创建您自己的生成器(我使用名称com.ourpackage.SybaseTableGenerator):

  import java.io.Serializable; 

import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.enhanced.TableGenerator;

@SuppressWarnings(UnusedDeclaration)
公共类SybaseTableGenerator扩展TableGenerator {
$ b $ @Override
public synchronized Serializable generate(SessionImplementor session,Object obj) {
return(Long)super.generate(session,obj)+ 1;






$ b

有点棘手, p>

I need to connect to a vendor db and insert customer data. The sequence table used to generate new customer ids stores the last used id (not the next available). I couldn't find anything in the jpa or hibernate docs that would indicate a way to tell hibernate to treat the id in the seq table as last used instead of next available (when using @TableGenerator).

Do I need to write a custom generator that will behave essentially the same as @TableGenerator with the only difference being the way the value in the sequence table is treated?

My Customer entity is defined as follows:

@Entity
public class Customer {

    @Id
    @TableGenerator(name = "cust_gen", table = "SEQUENCE", pkColumnName = "target",
        pkColumnValue = "customer", valueColumnName = "id", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "pat_gen")
    public long getCustomer_id() {
          return customer_id;
    }

    public void setCustomer_id(Long id) {
    this.customer_id = id;
}

    ...

}

Thanks!

解决方案

I had the same problem. Fixed it this way: Use Hibernate org.hibernate.annotations.GenericGenerator instead of persistance TableGenerator like this:

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "name")
public class Name implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "names")
@GenericGenerator(name = "names", strategy = "com.ourpackage.SybaseTableGenerator", parameters = {
        @Parameter(name = "table_name", value = "new_key_numbers"),
        @Parameter(name = "value_column_name", value = "key_number"),
        @Parameter(name = "segment_column_name", value = "name"),
        @Parameter(name = "segment_value", value = "names_key") })
@Column(name = "names_id")
private Long id;

And create your own generator (I used name com.ourpackage.SybaseTableGenerator):

import java.io.Serializable;

import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.enhanced.TableGenerator;

@SuppressWarnings("UnusedDeclaration")
public class SybaseTableGenerator extends TableGenerator {

    @Override
    public synchronized Serializable generate(SessionImplementor session, Object obj) {
        return (Long) super.generate(session, obj) + 1;
    }
}

A little bit tricky but it works ;)

这篇关于@TableGenerator可以保存表中最后使用的ID而不是下一个可用的ID吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆