Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate foreign key issue : Error executing DDL "alter table..."

I have a pretty simple object structure that is giving me an error I'm not able to resolve. Have done a bunch of searching, and I think this must be a pretty common use case, so not sure what the issue is. I have these three classes:

@Entity
public class Widget {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int idd;
    private Date date;
    private String type;
    private int ownerId;

    @OneToOne(cascade = CascadeType.ALL)
    private Rating rating;


    @OneToMany(cascade = CascadeType.ALL)
    private List<Tag> tagList;


}


@Entity
public class Rating {
public enum ChartType  {RADAR, BAR, LINE, STAR};

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int ratingId;
private String name;

@Enumerated(EnumType.STRING)
private ChartType chartType;
private double normalizedValue;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<RatingComponent> componentList = new ArrayList<>();


public Rating() {
    buildNormalizedValue();
}
}


@Entity
public class RatingComponent {


@Id
@GeneratedValue()
private int compId;

private String name;
private double value;
private double maxValue;

}

when saving in the DAO I'm getting errors I don't really understand, such as

2018-07-23 11:43:07,208 WARN o.h.t.s.i.ExceptionHandlerLoggedImpl [main] GenerationTarget encountered exception accepting command : 
Error executing DDL "alter table Rating_RatingComponent drop foreign key FKaudjguwlo1i8tm2jgli5bbnq6" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table Rating_RatingComponent drop foreign key FKaudjguwlo1i8tm2jgli5bbnq6" via JDBC Statement

and further down

2018-07-23 11:43:07,644 WARN o.h.t.s.i.ExceptionHandlerLoggedImpl [main] GenerationTarget encountered exception accepting command : Error executing DDL "create table RatingComponent (compId integer not null, maxValue double precision not null, name varchar(255), value double precision not null, primary key (compId)) engine=InnoDB" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table RatingComponent (compId integer not null, maxValue double precision not null, name varchar(255), value double precision not null, primary key (compId)) engine=InnoDB" via JDBC Statement

I have these properties in my config

    <property name="hibernate.hbm2ddl.auto">create</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 

I thought I had gone thru the docs pretty carefully, and think this is a pretty straight forward object design...any ideas as to how to resolve, and where in the docs should I research more carefully to understand the root cause?

thanks!

like image 910
badperson Avatar asked Jul 23 '18 15:07

badperson


2 Answers

I had used an SQL reserved word 'order' as the entity name, due to which I was getting a similar error. Renaming it solved it for me. Look at your entity names, have you used any reserved word in them?

like image 89
Mohammed Siddiq Avatar answered Sep 19 '22 15:09

Mohammed Siddiq


replacing

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

by

<property name="hibernate.dialect">org.hibernate.dialect.MySQL8InnoDBDialect</property>

will do the trick

like image 40
Svenmarim Avatar answered Sep 21 '22 15:09

Svenmarim