双主键关联映射(double primary key)

A Foreign key refering com.wawagame.backend.trade.hbentity.ProductEntity from com.wawagame.backend.trade.hbentity.OrdersEntity has the wrong number of column. should be 2

当一个表里有双主键:

在进行多对一映射时:

当该表作为one的一方是时:

再多的一方的entity里用注解进行映射要关联两个主键:

例子如下:

OrdersEntity 多的一方

@Entity
@Table(name = "orders", schema = "", catalog = "game")
public class OrdersEntity {
@Id
@Column(name = "serialNumber")
private String serialNumber;
@Basic
@Column(name="userId")
private String userId;
@Basic
@Column(name = "appId")
private String appId;
@Basic
@Column(name = "productId")
private long productId;
@Basic
@Column(name = "quantity")
private long quantity;
@Basic
@Column(name = "price")
private long price;
@Basic
@Column(name = "status")
private byte status;
@Basic
@Column(name = "paymentPlat")
private String paymentPlat;
@Basic
@Column(name = "createTime")
private long createTime;
@Basic
@Column(name = "paymentTime")
private Long paymentTime;
@Basic
@Column(name = "totalPrice")
private long totalPrice;
@Basic
@Column(name = "clientIp")
private String clientIp;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "appId",insertable = false,updatable = false)
private AppEntity appEntity;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "productId", insertable = false, updatable = false),
@JoinColumn(name = "appId", insertable = false, updatable = false)
})
private ProductEntity productEntity;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userid",insertable = false,updatable = false)
private UserEntity userEntity;

 PoductEntity一的一方

public class ProductEntityPK implements Serializable {
@Column(name="productId")
@Id
private long productId;
@Column(name="appId")
@Id
private String appId;

@Entity
@Table(name = "product", schema = "" , catalog = "game")
@IdClass(ProductEntityPK.class)
public class ProductEntity {
@Id
@Column(name = "appId")
private String appId;
@Id
@GeneratedValue
@Column(name = "productId")
private long productId;
@Basic
@Column(name = "productName")
private String productName;
@Basic
@Column(name = "productDesc")
private String productDesc;
@Basic
@Column(name = "price")
private long price;
@OneToMany(mappedBy = "productEntity",fetch = FetchType.LAZY)
private Set<OrdersEntity> ordersEntity = new HashSet<OrdersEntity>();
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="appId",insertable = false, updatable = false)
private AppEntity appEntity;

原文地址:https://www.cnblogs.com/sy-liu/p/6890660.html