admin管理员组文章数量:1279236
I have two SQL tables with composite primary and foreign keys, and I’m using Hibernate to manage the entities and their relationship between these tables. However, I’m encountering the following exception when trying to run the application:
Exception:
Caused by: .hibernate.MappingException: Foreign key (FK84h43wmylqljie636t5tprjqv:PRODUCT_DETAIL [PRODUCT_PARTITION_DATE,ID_PRODUCT])) must have same number of columns as the referenced primary key (PRODUCT [ID])
at .hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:149)
at .hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:131)
at .hibernate.boot.internal.InProductMetadataCollectorImpl.secondPassCompileForeignKeys(InProductMetadataCollectorImpl.java:1856)
at .hibernate.boot.internal.InProductMetadataCollectorImpl.secondPassCompileForeignKeys(InProductMetadataCollectorImpl.java:1772)
at .hibernate.boot.internal.InProductMetadataCollectorImpl.processSecondPasses(InProductMetadataCollectorImpl.java:1633)
at .hibernate.boot.model.process.spi.MetadataBuildingProcessplete(MetadataBuildingProcess.java:295)
at .hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1460)
at .hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1494)
Here’s the structure of my SQL tables, product and product_detail: Table product:
CREATE TABLE IF NOT EXISTS company_inventory.product
(
product_id bigint NOT NULL,
product_partition_date timestamp without time zone NOT NULL,
CONSTRAINT product_pkey PRIMARY KEY (product_id, product_partition_date),
-- other fields...
);
Table product_detail (modified):
CREATE TABLE IF NOT EXISTS company_inventory.product_detail
(
detail_id bigint NOT NULL,
product_id bigint,
product_partition_date timestamp without time zone,
detail_partition_date timestamp without time zone NOT NULL,
CONSTRAINT product_detail_pkey PRIMARY KEY (detail_id, detail_partition_date),
CONSTRAINT product_detail_fk1 FOREIGN KEY (product_id, product_partition_date) references product (product_id, product_partition_date),
-- other fields...
);
Java Entities:
Class ProductId:
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class ProductId implements Serializable {
private Long productId;
private Date productPartitionDate;
}
Entity Product:
@Entity
@Table(name = "PRODUCT")
@IdClass(ProductId.class)
@RequiredArgsConstructor
public class Product extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_PRODUCT")
@SequenceGenerator(name = "SEQ_PRODUCT", sequenceName = "SEQ_PRODUCT", allocationSize = 1)
@Column(name = "PRODUCT_ID")
private Long productId;
@Id
@Temporal(TemporalType.DATE)
@Column(name = "PRODUCT_PARTITION_DATE", nullable = false)
private Date productPartitionDate;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "product")
private List<ProductDetail> productDetailList;
@Embedded
private ProductKey productKey;
// other fields
}
Class ProductDetailId:
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class ProductDetailId implements Serializable {
private Long detailId;
private Date detailPartitionDate;
}
Entity ProductDetail:
@Entity
@Table(name = "PRODUCT_DETAIL")
@IdClass(ProductDetailId.class)
@Getter
@Setter
public class ProductDetail extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_PRODUCT_DETAIL")
@SequenceGenerator(name = "SEQ_PRODUCT_DETAIL", sequenceName = "SEQ_PRODUCT_DETAIL", allocationSize = 1)
@Column(name = "DETAIL_ID")
private Long detailId;
@Id
@Temporal(TemporalType.DATE)
@Column(name = "DETAIL_PARTITION_DATE", nullable = false)
private Date detailPartitionDate;
@ManyToOne
@JoinColumns({
@JoinColumn(name = "product_id", referencedColumnName = "product_id"),
@JoinColumn(name = "product_partition_date", referencedColumnName = "product_partition_date")
})
private Product product;
// other fields
}
I’m currently stuck with this issue. I’ve searched everywhere for a solution, but the problem persists
Normally, I only have an ID as the primary key for each table. I added a column for partitioning because I am using pg_partman as an extension on PostgreSQL, which requires me to use a composite key in my tables.
本文标签: javaForeign Key Mapping Exception in Hibernate Column MismatchStack Overflow
版权声明:本文标题:java - Foreign Key Mapping Exception in Hibernate: Column Mismatch - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741304126a2371266.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论