简体   繁体   中英

@ManyToMany Join Table fails

I have run into a problem that I haven't been able to solve yet.

Environment:

  • MySQL 5
  • OS X
  • Hibernate 4.1
  • Spring 3.1
  • Spring Data JPA

I have two entities in a ManyToMany relationship. I use a Join table with foreign keys for this. Here are my entities (modified for brevity):

@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>();
}

AND

@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>();

My join table is:

    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

I am using this naming:

hibernate.ejb.naming_strategy=org.hibernate.cfg.DefaultNamingStrategy

when I try to use this:

Employee emp = new Employee();
...
emp.getOffices().add(newOffice);
employeeRepository.save(emp);

the last insert statement before failure is:

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]

It appears that the case is all changed, and I see that someone thinks this might be a MySQL on OS X issue. but when I use

hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy

that doesn't fix it for me.

So does anyone know if this is a naming strategy issue or something else?

I'd expect a different error though, but the fields defined in the JoinColumn do not seem to match what you've specified as fields in the relation table. Try fixing the join columns in the annotation to refer to "Employee_id" and "Office_id" instead of "EmployeeId" and "OfficeId".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM