我有 Hibernate 映射的类,其中 ManyToOne 注释有时会导致数据库中的外键约束(我已经尝试过 Oracle 和 H2),有时不会。
例如,这里没有为 source_id 生成约束:
@Entity
@Table( name = "onto_entry" )
@Inheritance ( strategy = InheritanceType.SINGLE_TABLE )
@DiscriminatorColumn ( name = "term_category" )
@DiscriminatorValue ( "generic" )
@SequenceGenerator( name = "hibernate_seq", sequenceName = "onto_entry_seq" )
public class OntologyEntry extends Identifiable
{
...
@ManyToOne ( targetEntity = ReferenceSource.class, cascade = {
CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH } )
@JoinColumn( name = "source_id" )
public ReferenceSource getSource ()
...
}
虽然我在这里为 protocol_id 生成了 FK 约束:
@Entity
@Table ( name = "protocol_application" )
public class ProtocolApplication extends Identifiable
{
@ManyToOne ( cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, targetEntity = Protocol.class )
@JoinColumn ( name = "protocol_id" )
public Protocol getProtocol ()
...
}
难道我做错了什么?哪些因素会影响这种行为?