休眠自动递增序列在删除和插入后丢失 [英] Hibernate auto increment sequence is lossing after delete and insert

查看:88
本文介绍了休眠自动递增序列在删除和插入后丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的应用程序使用SpringMVC + Hibernate + Mysql组合



我从一开始就抱怨一个问题 -



在我的应用程序中,一些具有自动递增功能的表格使用Hibernate.So从这个tble添加,当我删除任何记录,然后当我再次插入一些时,我没有得到正确的序列 p>

例如。
假设我的表ABC最初有100条记录(例如.abc_id的主键,其值为从1到100的正确顺序)。



现在当我删除例如。 50条记录,并再次插入50条记录,但是我得到的这个时间序列是从101开始的,即使我有相同的100条记录。
所以我需要记录应该开始后,最后的Id值是从51到100再次。
所以在这种情况下,我的表正在失序,这在我的应用程序中非常常见。



以下是我的POJO设计

  @Entity 
@Table(name =outlet_info,catalog =secondary_sales)
public class OutletInfo实现java.io.可序列化{
// Fields
private static final long serialVersionUID = 7915773659216514833L;
private Integer outletId; // autoIncrement字段
private String outletTypeInfo;
private String outletName;
private String outletGrade;
$ b $ //构造函数

/ **默认构造函数* /
public OutletInfo(){
}

@Id
@GenericGenerator(name =generator,strategy =increment)
@GeneratedValue(generator =generator)
@Column(name =outlet_id,unique = true,nullable = false)
public Integer getOutletId(){
return this.outletId;
}

public void setOutletId(Integer outletId){
this.outletId = outletId;
}

//其他getter和setters
}



因此,在我的情况下,当通过使用Hibernate session.delete()函数的应用程序或直接SQL删除查询来删除任何Outlet(POJO)行时,我会在两种情况下丢失序列。
需要一些建议在这里可以做些什么来避免这种情况。



谢谢

解决方案

自动增量不保证顺序编号。这不是它的目的。它的目的是以最具负载效率的方式提供唯一性,因此它将始终返回最高的可用数量。

您想要回收的id可能已被其他行使用并试图提供一些机制来处理它并不那么简单。

事实上,你应该考虑你的代理键就是它们的意思:唯一的标识符没有其他的语义。如果你想要一些顺序编号,你可以自由地写你自定义逻辑,并将它应用到(最好)一些额外的列。



另见


I am using SpringMVC+Hibernate+Mysql combination for my application

I am stugling with one problem from start is -

"In my application some tables having auto-increment feature added using Hibernate.So from that tble when I am deleting any records and then when I am inserting again some more, I am not getting the proper sequence "

For eg. suppose my table ABC initially have 100 records (primary key for eg. abc_id having value in proper sequence from 1 to 100) .

Now when I deleted for eg. 50 records and again inserted 50 more records but this time sequence I am getting is starting from 101 even though I am having same 100 records. So I need record should start after last Id value that is from 51 to 100 again. So in such situations my tables are loosing sequence and this is very frequent case in my application.

Following is my POJO design

@Entity
@Table(name = "outlet_info", catalog = "secondary_sales")
public class OutletInfo implements java.io.Serializable {
    // Fields
    private static final long serialVersionUID = 7915773659216514833L;
    private Integer outletId;//autoIncrement fields
    private String outletTypeInfo;
    private String outletName;
    private String outletGrade;

    // Constructors

    /** default constructor */
    public OutletInfo() {
    }

    @Id
    @GenericGenerator(name="generator", strategy="increment")
    @GeneratedValue(generator="generator")
    @Column(name = "outlet_id", unique = true, nullable = false)
    public Integer getOutletId() {
        return this.outletId;
    }

    public void setOutletId(Integer outletId) {
        this.outletId = outletId;
    }

    //other getters and setters
}

So in my case, when i m deleting any Outlet(POJO) row either "through application using Hibernate session.delete() function" or direct "SQL delete query", I m loosing sequence in both case. Needed some suggestions what can be done here to avoid such case.

thanks

解决方案

Auto increment does not guarantee sequential numbering. This is not its purpose. Its purpose is to provide uniqueness in the most load-efficient way, so it will always return the highest available number.
The ids you want to reclaim might have already been used by other rows and trying to provide some mechanism to cope with it is not that simple.
In fact you should consider your surrogate keys just what they meant to be: unique identifiers with no other semantics. If you want some sequential numbering you are free to write you custom logic and apply it to (preferably) some additional column.

See also this

这篇关于休眠自动递增序列在删除和插入后丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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