gpt4 book ai didi

java - 如何使用 OpenJPA 映射关系 Store <- Stock -> Product?

转载 作者:行者123 更新时间:2023-11-30 11:16:48 24 4
gpt4 key购买 nike

我有下一段关系:

Relationship

目前,我有下一个代码:

@Embedded
public class StockPK implements Serializable {

private int storeId;

private int productId

}

@Entity
public class Stock implements Serializable {

@EmbeddedId
private StockPK id;

private int cantidad;

@ManyToOne
private Store store;

@ManyToOne
private Product product;

}

但是生成的 DDL(我在 TomEE 中使用 OpenJPA)添加了两个附加字段。

CREATE TABLE STOCK (
productId INTEGER NOT NULL,
storeId INTEGER NOT NULL,
quantity INTEGER NOT NULL,
PRODUCT_ID INTEGER ,
STORE_ID INTEGER ,
PRIMARY KEY (productId, storeId)
)

应该如何指定这种关系?

最佳答案

感谢 JBNizet :) — 解决方案如下:

@Embeddable
public class StockPK implements Serializable {

@Column(name = "store_id")
private int storeId;

@Column(name = "product_id")
private String productId;

// Getters, setters, hashCode, equals

}
@Entity
@Table(name = "stock")
public class Stock implements Serializable {

@EmbeddedId
private StockPK id;

@MapsId("storeId")
@ManyToOne
private Store store;

@MapsId("productId")
@ManyToOne
private Product product;

@Column(nullable = false)
private int quantity;

// Getters, setters

}
@Entity
@Table(name = "store")
public class Store implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

// Other fields, getters, setters ...

}
@Entity
@Table(name = "product")
public class Product implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

// Other fields, getters, setters ...

}

关于java - 如何使用 OpenJPA 映射关系 Store <- Stock -> Product?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24683400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com