我遇到了一个我还不能解决的问题。
环境:
- MySQL 5
- 操作系统
- 休眠 4.1
- 春天 3.1
- 春季数据 JPA
我在多对多关系中有两个实体。为此,我使用带有外键的联接表。这是我的实体(为简洁而修改):
@Entity
public class Employee {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
private Long employeeId;
@ManyToMany( cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = Office.class )
@JoinTable( name = "EmployeeOfficeJoin", joinColumns = { @JoinColumn( name = "EmployeeId", nullable = false, updatable = false ) }, inverseJoinColumns = { @JoinColumn( name = "OfficeId", nullable = false, updatable = false ) } )
private List<Office> offices = new ArrayList<Office>();
}
和
@Entity
public class Office {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
private Long officeId;
@ManyToMany( mappedBy = "offices", targetEntity = Employee.class, fetch = FetchType.EAGER )
private List<Employee> employees = new ArrayList<Employee>();
我的联接表是:
CREATE TABLE IF NOT EXISTS `dental`.`EmployeeOfficeJoin` (
`EmployeeId` BIGINT(11) ,
`OfficeId` BIGINT(11),
PRIMARY KEY ( `employeeId`, `officeId` ),
FOREIGN KEY ( `employeeId` ) REFERENCES `Employee` (`EmployeeId` ),
FOREIGN KEY ( `officeId` ) REFERENCES `Office` ( `Officeid` ) )
ENGINE = InnoDB
我正在使用这个命名:
hibernate.ejb.naming_strategy=org.hibernate.cfg.DefaultNamingStrategy
当我尝试使用它时:
Employee emp = new Employee();
...
emp.getOffices().add(newOffice);
employeeRepository.save(emp);
失败前的最后一个插入语句是:
Hibernate:
insert
into
EmployeeOfficeJoin
(EmployeeId, OfficeId)
values
(?, ?)
TRACE - BasicBinder - binding parameter [1] as [BIGINT] - 1
TRACE - BasicBinder - binding parameter [2] as [BIGINT] - 1
DEBUG - SqlExceptionHelper - Cannot add or update a child row: a foreign key constraint fails (`dental`.`employeeofficejoin`, CONSTRAINT `employeeofficejoin_ibfk_1` FOREIGN KEY (`EmployeeId`) REFERENCES `Employee` (`EmployeeId`)) [n/a]
看来情况已经全部改变了,我看到有人认为这可能是 OS X 上的 MySQL 问题。但是当我使用
hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
这并不能解决我的问题。
那么有人知道这是命名策略问题还是其他问题?