我正在尝试在两个表之间实现一对多关系:
主表商家
@Entity
@Table
public class Merchants {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private int id;
@Column
private String name;
@OneToMany(mappedBy="id")
private List<Terminals> terminals;
@Column
private String login;
}
包含商户ID的第二表终端
@Entity
@Table
public class Terminals {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private int id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="merchant_id")
private Merchants merchants;
@Column
private String mode;
}
但是随后我部署了得到异常的代码:
Caused by: java.sql.SQLSyntaxErrorException: (conn=163) Table 'production.terminals' doesn't exist
Caused by: java.sql.SQLException: Table 'production.terminals' doesn't exist
Query is: alter table terminals drop foreign key FKq779ocbhe9oeeor5591t31vx1
您知道如何解决此问题吗?
答案 0 :(得分:2)
使用以下代码
@Entity
@Table
public class Merchants {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "merchantId", updatable = false, nullable = false)
private int merchantId;
@Column
private String name;
@OneToMany(mappedBy="merchants")
private List<Terminals> terminals;
@Column
private String login;
}
@Entity
@Table
public class Terminals {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private int id;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="merchantId")
private Merchants merchants;
@Column
private String mode;
}