1

まだ解決できていない問題に遭遇しました。

環境:

  • MySQL 5
  • OS X
  • 休止状態 4.1
  • 春 3.1
  • 春のデータ JPA

ManyToMany 関係に 2 つのエンティティがあります。これには、外部キーを持つ結合テーブルを使用します。これが私のエンティティです(簡潔にするために変更されています):

@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

それは私にとってそれを修正しません。

これが命名戦略の問題なのか、それとも何か他の問題なのか、誰にもわかりますか?

4

1 に答える 1

0

別のエラーが予想されますが、JoinColumnで定義されたフィールドは、リレーションテーブルのフィールドとして指定したものと一致していないようです。アノテーションの結合列を修正して、「EmployeeId」と「OfficeId」ではなく「Employee_id」と「Office_id」を参照するようにしてください。

于 2012-11-08T15:24:26.747 に答える