我用:
这是SQL-Table-Definition:
CREATE TABLE salaries (
emp_no BIGINT NOT NULL,
salary BIGINT NOT NULL,
from_date DATETIME NOT NULL,
to_date DATETIME NOT NULL,
FOREIGN KEY (emp_no) REFERENCES employees (emp_no),
PRIMARY KEY (emp_no, from_date)
)
这是关联多方面的实体(使用嵌入式ID):
@Entity
@Table(name = "salaries")
public class Salary {
@EmbeddedId
private EmpIdFromDatePK empId;
@Column(name = "to_date", columnDefinition = "DATETIME")
@Temporal(TemporalType.TIMESTAMP)
private Date toDate;
@Column(name = "salary")
private Long salary;
@ManyToOne(fetch = FetchType.LAZY)
@Fetch(FetchMode.JOIN)
@MapsId("empId")
@JoinColumns({@JoinColumn(foreignKey = @ForeignKey(name = "salaries_ibfk_1"), name = "emp_no", referencedColumnName = "emp_no")})
private Employee employee;
public Salary() {
super();
}
//...
}
关键课程:
@Embeddable
public class EmpIdFromDatePK implements Serializable {
@Column(name = "emp_no")
private Long empId;
@Column(name = "from_date", columnDefinition = "DATETIME")
@Temporal(TemporalType.TIMESTAMP)
private Date fromDate;
public EmpIdFromDatePK() {
super();
}
//....
}
该协会一方的实体:
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "emp_no")
private Long empId;
//...
@OneToMany(mappedBy = "employee")
private List<Salary> salaryList;
//...
}
问题:当我使用ManyToOne关联时(例如,在员工加入工资记录的选择中),在数据库中创建了第二个外键:
CONSTRAINT `FKjojacp79fphmajxrdll8fvf5o` FOREIGN KEY (`emp_no`) REFERENCES `employees` (`emp_no`),
CONSTRAINT `salaries_ibfk_1` FOREIGN KEY (`emp_no`) REFERENCES `employees` (`emp_no`)
问题:我该如何避免这种行为?
提前致谢
答案 0 :(得分:2)
你可以尝试:
Template parse errors:
Can't bind to 'ngSwitchWhen' since it isn't a known property of 'form'.
答案 1 :(得分:1)
JPA 2.1仅支持通过@ForeignKey注释定义外键。
所以如果你使用JPA旧版本而不是2.1,我认为你不能这样做