eclipselink并不关心外键约束,但DBMS确实如此

时间:2015-05-12 07:56:46

标签: java mysql jpa foreign-keys eclipselink

我遇到了JPA和关系manyToMany的问题。我对关系的两个方面都使用了继承策略。 Jpa没有考虑外键约束,但是当我使用workbensh删除绑定到电影的字符时,mysql会这样做。

我不明白JPA如何通过约束。

第一类层次结构:

@Entity
@DiscriminatorColumn(name = "TYPEOEUVRE",discriminatorType = 
DiscriminatorType.INTEGER)
@Table(name="Oeuvre")

public abstract class EtyOeuvre extends EtyAbstract implements 
ItCompositeInfo,Comparable<EtyOeuvre> {

    @ManyToMany(cascade ={ CascadeType.PERSIST},fetch = FetchType.EAGER)
    @JoinTable(name="oeuvres_characters", joinColumns = 
              @JoinColumn(name="oeuvres_ID"),inverseJoinColumns = 
              @JoinColumn(name="characters_ID"))
    @OrderBy("last_name,first_name ASC")
    protected List<EtyCharacter> characters  = new 
                                 LinkedList<EtyCharacter>();

}

@MappedSuperclass 
public class EtyOeuvreCinematographic extends EtyOeuvre {

}

@Entity @DiscriminatorValue("1") 
 public class EtySerieTv extends EtyOeuvreCinematographic {

}

第二类层次结构

@Entity
@DiscriminatorColumn(name = "TYPEHUMAN",discriminatorType = 
public abstract class EtyHuman extends EtyAbstract implements 
Comparable<EtyHuman>{

}


@Entity
@DiscriminatorValue("0")
public class EtyCharacter extends EtyHuman {

    @ManyToMany(mappedBy = "characters",cascade = CascadeType.PERSIST,fetch 
                = FetchType.EAGER)
    private List<EtyOeuvre> oeuvres;
}

这是连接表的DLL代码

CREATE TABLE `oeuvres_characters` (
  `characters_ID` int(11) NOT NULL,
  `oeuvres_ID` int(11) NOT NULL,
  PRIMARY KEY (`characters_ID`,`oeuvres_ID`),
  KEY `FK_oeuvres_characters_oeuvres_ID` (`oeuvres_ID`),
  CONSTRAINT `FK_oeuvres_characters_characters_ID` FOREIGN KEY 
 (`characters_ID`) REFERENCES `human` (`ID`),
  CONSTRAINT `FK_oeuvres_characters_oeuvres_ID` FOREIGN KEY (`oeuvres_ID`) 
REFERENCES `oeuvre` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
oeuvres_characters

我在代码中真正使用的类是EtyCharacter,例如EtySerieTv。

感谢您的帮助:)

修改。

这是删除字符时生成的SQl:

  DELETE FROM oeuvres_characters WHERE ((characters_ID = ?) AND (oeuvres_ID 
  = ?))
    bind => [76, 1]]]

 DELETE FROM human WHERE ((ID = ?) AND (VERSION = ?))
   bind => [76, 1]]]

你可以看到它之前删除了join_table引用。我预先获得外键约束e ^ ^^。

0 个答案:

没有答案