0

Full error is

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table ValidationStep add constraint FKkawiwe2rg1ar6xla3y4gen5aq foreign key (validationStepDraftGroup_id) references ValidationStepDraftGroup (id)" via JDBC Statement

It is about my oneToMany mapping of ValidationStepDraftGroup to validationStep. I appreciate any help

ValidationStepDraftGroup entity

 @Entity
 public class ValidationStepDraftGroup {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true,fetch=FetchType.LAZY)
@JoinColumn(name = "validationStepDraftGroup_id")
private List<ValidationStep> validationSteps;
// no cascade type, you shouldnt be able to modify a automationInformation by saving a ValidationStepDraftGroup
@ManyToOne
@JoinColumn(name= "automation_information_aId", referencedColumnName= "aId")
private AutomationInformation automationInformation;

private String lastUpdatedBy;

private Date lastUpdatedDate;
}

ValidationStep

@Entity
public class ValidationStep {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;



@ManyToOne
@JoinColumn(name= "automation_information_aId", referencedColumnName= "aId")
private AutomationInformation automationInformation;


private int sequence;

public enum ValidationType {
    Email, Document, Sql, Custom
};

@Enumerated(EnumType.STRING)
private ValidationType type;

@OneToOne(cascade = { CascadeType.ALL }, orphanRemoval = true,fetch=FetchType.LAZY)
private EmailDetail emailDetail;
//because the object is lazily fetched
//jackson(the library which converts objects to json) tries to serialize the validation step object before they are fetched
// resulting in an error
// this fixes it
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@OneToOne(cascade = { CascadeType.ALL }, orphanRemoval = true,fetch=FetchType.LAZY)
private DocumentDetail documentDetail;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@OneToOne(cascade = { CascadeType.ALL }, orphanRemoval = true,fetch=FetchType.LAZY)
private SqlDetail sqlDetail;

private String description;

@Column(columnDefinition = "boolean default false")
private Boolean isDraft;

Relevant configuration

spring.datasource.type = org.apache.tomcat.jdbc.pool.DataSource
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Daniel Haughton
  • 1,085
  • 5
  • 20
  • 45

1 Answers1

0

Well, based on the information gathered in your comments, I think the problem is on the fact that 'type' is a reserved word on MySQL.

Could you please try to add a @Column annotation to the property type in ValidationStep and test it again?

Example:

@Enumerated(EnumType.STRING)
@Column(name = "validationType")
private ValidationType type;

Update

Change from

spring.jpa.hibernate.ddl-auto=create

to

spring.jpa.hibernate.ddl-auto=update
Matheus
  • 3,058
  • 7
  • 16
  • 37
  • The "DraftGroup" object is an addition, previously ValidaionStep has been working fine as posted. So I would think that means it is not the problem? – Daniel Haughton Oct 15 '19 at 11:37
  • Well, test it, if you can. If it doesn't work, just drop the changes – Matheus Oct 15 '19 at 11:38
  • Still the same issue – Daniel Haughton Oct 15 '19 at 11:40
  • Also still the same issue – Daniel Haughton Oct 15 '19 at 11:51
  • I'm unable to reproduce your error. Your schema is working for me. Try changing the dialect to `org.hibernate.dialect.MySQL5InnoDBDialect` and `org.hibernate.dialect.MySQL8InnoDBDialect`. Test with both – Matheus Oct 15 '19 at 12:14
  • So for MysqlInnofb I get the error Could not load requested class : org.hibernate.dialect.MySQL8InnoDBDialect. For org.hibernate.dialect.MySQL5InnoDBDialect and org.hibernate.dialect.MySQL8Dialect the issue is still the same – Daniel Haughton Oct 15 '19 at 13:27
  • I recall a while ago coming accros a "Known issue" in spring data JPA where too many children relationships caused an error. I wonder if this that error? I cant find the old bug report now. But my ValidationStep has an SqlDetail, and SqlDetail has a another one to many collection of SqlValidations. And same for documentDetail/documentValidatins – Daniel Haughton Oct 15 '19 at 13:29