提问人:Almat Rakhmetolla 提问时间:3/23/2022 更新时间:3/23/2022 访问量:867
Hibernate allocationSize 忽略 “allocationSize”
Hibernate allocationSize ignores "allocationSize"
问:
如何整理休眠和数据库序列生成?
我的实体:
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]
如何解决?请帮忙!
答:
0赞
Almat Rakhmetolla
3/23/2022
#1
我通过以下方式解决了更换发电机的问题:
@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();
}
}
评论
sec_role_seq
@SequenceGenerator(sequenceName = "SEC_ROLE_ID_SEQ"