Ausnahme von Fremd Key Mapping in Hibernate: SäulenfehlanpassungJava

Java-Forum
Anonymous
 Ausnahme von Fremd Key Mapping in Hibernate: Säulenfehlanpassung

Post by Anonymous »

Ich habe zwei SQL -Tabellen mit zusammengesetzten Primär- und Fremdschlüssel, und ich verwende Hibernate, um die Entitäten und ihre Beziehung zwischen diesen Tabellen zu verwalten. Ich begegne jedoch die folgende Ausnahme, wenn ich versuche, die Anwendung auszuführen: < /p>
Ausnahme: < /p>
Caused by: org.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 org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:149)
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:131)
at org.hibernate.boot.internal.InProductMetadataCollectorImpl.secondPassCompileForeignKeys(InProductMetadataCollectorImpl.java:1856)
at org.hibernate.boot.internal.InProductMetadataCollectorImpl.secondPassCompileForeignKeys(InProductMetadataCollectorImpl.java:1772)
at org.hibernate.boot.internal.InProductMetadataCollectorImpl.processSecondPasses(InProductMetadataCollectorImpl.java:1633)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:295)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1460)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1494)
< /code>
Hier ist die Struktur meiner SQL -Tabellen, Produkte und product_detail:
Tabellenprodukt: < /p>
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...
);
< /code>
table product_detail (geändert): < /p>
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...
);
< /code>
Java -Entitäten: < /p>
Class ProductId:
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class ProductId implements Serializable {
private Long productId;
private Date productPartitionDate;
}
< /code>
Entitätsprodukt: < /p>
@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
productDetailList;

@Embedded
private ProductKey productKey;
// other fields
}
< /code>
Klasse productDetailId: < /p>
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class ProductDetailId implements Serializable {
private Long detailId;
private Date detailPartitionDate;
}
< /code>
Entity productDetail: < /p>
@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
}
< /code>
Ich bin derzeit an diesem Problem festgefahren. Ich habe überall nach einer Lösung gesucht, aber das Problem besteht weiterhin < /p>
Normalerweise habe ich nur eine ID als Primärschlüssel für jede Tabelle. Ich habe eine Spalte zur Partitionierung hinzugefügt, da ich PG_PARTMAN als Erweiterung auf PostgreSQL verwende, wodurch ich einen zusammengesetzten Schlüssel in meinen Tabellen verwenden muss.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post