Pregunta

I have Hibernate-mapped classes, where ManyToOne annotations sometimes result in a foreign key constraint in the database (I've tried both Oracle and H2), sometimes don't.

For instance, no constraint is generated for source_id here:

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

While I get a FK constraint generated for protocol_id here:

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

Am I doing something wrong? Which elements can influence such behaviour?

¿Fue útil?

Solución

Going to answer myself.

Hibernate doesn't create join table indexes in Oracle, unless in create mode. So I've decided to do it myself, partially automating it by looking at JPA annotations. This is what I've done so far.

Otros consejos

Probably hibernate.dialect is set to MyISAM instead of InnoDB

MyISAM doesn't support foreign keys.

So set

hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

or for Spring Boot

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top