休眠外键问题:执行DDL“更改表...”时出错

时间:2018-07-23 15:49:51

标签: java hibernate hibernate-mapping

我有一个非常简单的对象结构,这给了我一个我无法解决的错误。已经做了很多搜索,我认为这一定是一个非常普遍的用例,所以不确定是什么问题。我有这三个班级:

@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;

}

在保存DAO时,我遇到了我不太了解的错误,例如

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

再往下

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

我的配置中有这些属性

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

我认为我已经非常仔细地通过了文档,并且认为这是一个非常直接的对象设计...有关如何解决的任何想法,以及我应该在文档中的哪些位置进行更仔细的研究以了解根本原因?

谢谢!

4 个答案:

答案 0 :(得分:0)

替换

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

通过

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

会成功的

答案 1 :(得分:0)

我必须删除所有表才能使其正常工作。另外,请检查您是否使用保留字作为表名。

答案 2 :(得分:0)

在我的情况下,数据库用户没有足够的权限使用reference命令更改表。提示堆栈跟踪出现类似

的错误

由以下原因引起:java.sql.SQLSyntaxErrorException:向用户拒绝了REFERENCES命令

更新数据库用户权限可解决此问题

也将更新用作ddl-auto

jpa: 休眠: ddl-auto:更新

答案 3 :(得分:0)

由于使用了SQL保留字'order'作为实体名称,因此出现类似的错误。重命名它为我解决了。看一下您的实体名称,您是否在其中使用了保留字?