即使列不是主要键,也允许使用@generatedValue

发布于 2025-01-22 00:37:30 字数 2057 浏览 0 评论 0原文

我有一个实体类 supplierorder.java ,其中包含两个字段: cuid orderid

@Data
@EqualsAndHashCode(callSuper = false, of = { "cuId" })
@Builder(toBuilder = true)
@AllArgsConstructor
@Entity
@Table(name = "supplier_order")
public class SupplierOrder
{
        @Column(name = "cuid", nullable = false)
        private Long cuId;
    
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "supOrd")
        @SequenceGenerator(name = "supOrd", sequenceName = "supplier_order_seq", allocationSize = 1 )
        @Column(name = "order_id", nullable = false)
        private Integer orderId;
}

当我保存我的实体在数据库上的以下操作 现在承担

1. select next value for supplier_order_seq
2. insert into supplier_order(cuid,order_id)values(101,"<value from the point 1>")

,我必须翻转主要钥匙。换句话说,Cuid成为我的新主钥匙,其余的保持不变。

@Data
@EqualsAndHashCode(callSuper = false, of = { "orderId" })
@Builder(toBuilder = true)
@AllArgsConstructor
@Entity
@Table(name = "supplier_order")
public class SupplierOrder
{
        @Id                  //changed primary key from orderId to cuid
        @Column(name = "cuid", nullable = false)
        private Long cuId;
    
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "supOrd")
        @SequenceGenerator(name = "supOrd", sequenceName = "supplier_order_seq", allocationSize = 1 )
        @Column(name = "order_id", nullable = false)
        private Integer orderId;
}

现在,当我试图保存我的实体时,会发生以下操作:

1. insert into supplier_order(cuid,order_id)values(101,NULL)

注意:从seq supplier_order_seq 获取下一个值并没有发生,因此,这给了我错误无效的。即使更改主键后,我如何确保SEQ supplier_seord_seq 的order_id值应提供?

我尝试从下面的链接中引用多个解决方案,但它们都没有用。

I have an entity class SupplierOrder.java which contains two fields: cuId and orderId

@Data
@EqualsAndHashCode(callSuper = false, of = { "cuId" })
@Builder(toBuilder = true)
@AllArgsConstructor
@Entity
@Table(name = "supplier_order")
public class SupplierOrder
{
        @Column(name = "cuid", nullable = false)
        private Long cuId;
    
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "supOrd")
        @SequenceGenerator(name = "supOrd", sequenceName = "supplier_order_seq", allocationSize = 1 )
        @Column(name = "order_id", nullable = false)
        private Integer orderId;
}

When I save my entity the following operation on the database used to undertake

1. select next value for supplier_order_seq
2. insert into supplier_order(cuid,order_id)values(101,"<value from the point 1>")

Now, I have to flip the primary key. In other words, cuid becomes my new primary key and the rest remains the same.

@Data
@EqualsAndHashCode(callSuper = false, of = { "orderId" })
@Builder(toBuilder = true)
@AllArgsConstructor
@Entity
@Table(name = "supplier_order")
public class SupplierOrder
{
        @Id                  //changed primary key from orderId to cuid
        @Column(name = "cuid", nullable = false)
        private Long cuId;
    
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "supOrd")
        @SequenceGenerator(name = "supOrd", sequenceName = "supplier_order_seq", allocationSize = 1 )
        @Column(name = "order_id", nullable = false)
        private Integer orderId;
}

Now, when I am trying to save my entity the following operations happen:

1. insert into supplier_order(cuid,order_id)values(101,NULL)

Note: getting next value from the seq supplier_order_seq does not happen and because of this, it is giving me error as I cannot save order_id as null. How do I make sure that order_id value should be provided by seq supplier_order_seq even after changing the primary key?

I have tried referring multiple solutions from the link below but none of them worked.

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

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

发布评论

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

评论(1

苍暮颜 2025-01-29 00:37:30

2023年,不确定是否迟到了,如果任何人都需要,下面的解决方案就可以使用额外的表实体来生成ID
假设您在DB中有生成器,那么您可以在下面执行

      @GeneratedValue(generator = "table_id_seq_gen")
        @GenericGenerator(
                name = "table_id_seq_gen",
                strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
                parameters = {
                        @Parameter(name = SequenceStyleGenerator.SEQUENCE_PARAM, value = "table_id_seq"),
                        @Parameter(name = SequenceStyleGenerator.INCREMENT_PARAM, value = "10"),
// other params if needed
                })
        @Generated(GenerationTime.INSERT) // here is the trick
        private Long id;

,如果您有序列号bigserial或sth,则可以执行

@Column(name = "id", columnDefinition = "serial")
@Generated(GenerationTime.INSERT)
private Long id;

键是@generated(generationTime.insert),因此,请总结

  • 如果未通过@generated指定的Defautl(generationTime)(generationTime)。插入) ,
    Hibernate仅为PK生成默认值。
  • 如果指定了
    使用需要运行时插入的任何列工作。
    @generated(generation time.insert)

2023, not sure if it's late to answer in case it's needed for anyone,below solution works without extra table entity for generating ids
assuming you have a generator in db, then you can do below

      @GeneratedValue(generator = "table_id_seq_gen")
        @GenericGenerator(
                name = "table_id_seq_gen",
                strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
                parameters = {
                        @Parameter(name = SequenceStyleGenerator.SEQUENCE_PARAM, value = "table_id_seq"),
                        @Parameter(name = SequenceStyleGenerator.INCREMENT_PARAM, value = "10"),
// other params if needed
                })
        @Generated(GenerationTime.INSERT) // here is the trick
        private Long id;

then if you have serial number bigSerial or sth you can do

@Column(name = "id", columnDefinition = "serial")
@Generated(GenerationTime.INSERT)
private Long id;

the key is @Generated(GenerationTime.INSERT), so to summarize

  • if by defautl not specified with @Generated(GenerationTime.INSERT) ,
    Hibernate only generates for PK as default.
  • if specified it would
    work with any column that needs a runtime insert.
    @Generated(GenerationTime.INSERT)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文