아직 해결할 수 없었던 문제가 발생했습니다.@ManyToMany Join Table이 실패합니다.
환경 :
- MySQL의 5
- OS X
- 최대 절전 모드 4.1
- 봄 3.1
- 스프링 데이터 JPA
내가 ManyToMany 관계에있는 두 개의 개체가 있습니다. 외부 키가있는 Join 테이블을 사용합니다. 여기 (간결하게 수정) 내 엔티티 :
@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>();
테이블을 조인 내 것은 :
hibernate.ejb.naming_strategy=org.hibernate.cfg.DefaultNamingStrategy
:이 이름을 사용하고
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
내가 시도 할 때
이것을 사용 : 실패가
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]
이 경우 모든 변경 것 같습니다, 나는 someone이는 MySQL의 OS X에 문제가 있다고 생각 것을 알 수있다. 그러나 사용할 때
hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
나는 그것을 고치지 않습니다.
이것이 이름 지정 전략 문제인지 아는 사람이 있습니까?
업데이트를 사용하도록 설정하지 않았습니까? – Thihara
"업데이트 사용"이란 무엇을 의미하는지 확실하지 않습니다. 나는 의도적으로 무언가를 무력화했다고 생각하지 않는다. – sonoerin