1.问题
- order_info表里面,采用product_id,order_id作为联合主键,还有一个属性id,想设置成自增长的非主键。如果在建表时sql语句里加上
key(id)
就可以解决。 - 但是,开发阶段,使用spring.jpa.hibernate.ddl-auto=create 根据entity自动生成mysql表,发现下面的配置无效,生成的表id 是没有auto_increment关键字的。
@IdClass(CommonPk.class)
public class OrderInfo{
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
private String productId;
private String orderId;
}
//其中,
class CommonPk implements Serializable{
private String productId;
private String orderId;
}
2.解决方法
- 也许不是最优解,但网上搜了一圈也没有个解决方案,也许是没找对地方,但自己想出了一个解决办法
- 如下,@Column加个columnDefinition属性
@Column(name = "id", columnDefinition = "bigint(20) not null UNIQUE key auto_increment")