即使列不是主要键,也允许使用@generatedValue
我有一个实体类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
2023年,不确定是否迟到了,如果任何人都需要,下面的解决方案就可以使用额外的表实体来生成ID
假设您在DB中有生成器,那么您可以在下面执行
,如果您有序列号bigserial或sth,则可以执行
键是@generated(generationTime.insert),因此,请总结
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
then if you have serial number bigSerial or sth you can do
the key is @Generated(GenerationTime.INSERT), so to summarize
Hibernate only generates for PK as default.
work with any column that needs a runtime insert.
@Generated(GenerationTime.INSERT)