我有一个非常简单的对象结构,它给我一个我无法解决的错误。做了很多搜索,我认为这一定是一个非常常见的用例,所以不确定问题是什么。我有这三个类:
@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>
我认为我已经非常仔细地阅读了文档,并且认为这是一个非常直接的对象设计......关于如何解决的任何想法,以及我应该更仔细地研究文档中的什么地方以了解根本原因?
谢谢!
最佳答案
我使用了 SQL 保留字 'order'
作为实体名称,因此我遇到了类似的错误。重命名它为我解决了它。看你的实体名,有没有用保留字?
关于java - hibernate 外键问题: Error executing DDL "alter table...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51482774/