Hibernate allocateSize 忽略“allocationSize”

发布于 2025-01-16 02:39:37 字数 1331 浏览 1 评论 0原文

如何整理hibernate和数据库序列生成?

我的实体:

package kz.open.sankaz.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.*;

@Entity
@Table(name = "SEC_ROLE")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SecRole extends AbstractEntity implements GrantedAuthority {
    @Id
    @GeneratedValue(generator = "SEC_ROLE_SEQ", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(allocationSize = 1, sequenceName = "SEC_ROLE_ID_SEQ", name = "SEC_ROLE_ID")
    private Long id;

    @Column(name = "NAME", nullable = false, unique = true)
    private String name;

    @Override
    public String getAuthority() {
        return getName();
    }
}

我写道“allocationSize”是1。但是Hibernate生成错误的查询:

Hibernate: create sequence public.sec_role_seq start 1 increment 50

它不仅在插入新数据时产生问题,而且在运行数据库迁移查询时也会产生问题。比如我写了下一行:

create sequence public.sec_role_seq start 1 increment 1;

和Hibernate冲突了:

The increment size of the [SEC_ROLE_SEQ] sequence is set to [50] in the entity mapping while the associated database sequence increment size is [1]

怎么解决?请帮忙!

How to put in order hibernate and database sequence generation?

My entity:

package kz.open.sankaz.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.*;

@Entity
@Table(name = "SEC_ROLE")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SecRole extends AbstractEntity implements GrantedAuthority {
    @Id
    @GeneratedValue(generator = "SEC_ROLE_SEQ", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(allocationSize = 1, sequenceName = "SEC_ROLE_ID_SEQ", name = "SEC_ROLE_ID")
    private Long id;

    @Column(name = "NAME", nullable = false, unique = true)
    private String name;

    @Override
    public String getAuthority() {
        return getName();
    }
}

I wrote that "allocationSize" is 1. But Hibernate generates wrong query:

Hibernate: create sequence public.sec_role_seq start 1 increment 50

It makes a problem not only while you are inserting a new data, it also makes problem when you are running database migration queries. For example, I wrote the next line:

create sequence public.sec_role_seq start 1 increment 1;

and Hibernate conflicts with it:

The increment size of the [SEC_ROLE_SEQ] sequence is set to [50] in the entity mapping while the associated database sequence increment size is [1]

How to solve it? Please, help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万人眼中万个我 2025-01-23 02:39:37

我通过以下方式解决了更改生成器的问题:

@Entity
@Table(name = "SEC_ROLE")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SecRole extends AbstractEntity implements GrantedAuthority {
    @Id
    @GeneratedValue(generator = "SEC_ROLE_SEQ", strategy = GenerationType.SEQUENCE)
    @GenericGenerator(
            name = "SEC_ROLE_SEQ",
            strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
            parameters = {
                    @Parameter(name = "sequence_name", value = "SEC_ROLE_SEQ"),
                    @Parameter(name = "initial_value", value = "1"),
                    @Parameter(name = "increment_size", value = "1")
            }
    )
    private Long id;

    @Column(name = "NAME", nullable = false, unique = true)
    private String name;

    @Override
    public String getAuthority() {
        return getName();
    }
}

I solved it changing generator in the following way:

@Entity
@Table(name = "SEC_ROLE")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SecRole extends AbstractEntity implements GrantedAuthority {
    @Id
    @GeneratedValue(generator = "SEC_ROLE_SEQ", strategy = GenerationType.SEQUENCE)
    @GenericGenerator(
            name = "SEC_ROLE_SEQ",
            strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
            parameters = {
                    @Parameter(name = "sequence_name", value = "SEC_ROLE_SEQ"),
                    @Parameter(name = "initial_value", value = "1"),
                    @Parameter(name = "increment_size", value = "1")
            }
    )
    private Long id;

    @Column(name = "NAME", nullable = false, unique = true)
    private String name;

    @Override
    public String getAuthority() {
        return getName();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文