我有下一个关系:
目前,我有下一个代码:
@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)
)
应该如何指定这种关系?
答案 0 :(得分:1)
感谢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 ...
}