我创建了一个具有表名和列名的实体。 我还为列名添加了uniquekey约束。但是当我运行时,它显示以下错误;
org.hibernate.tool.schema.spi.CommandAcceptanceException:错误 执行“ DDL”更改表成分添加约束 UK_co7ro6kyijhfik027h0y4d3n3唯一(ingredient_name)。
java.sql.SQLSyntaxErrorException:指定的键太长;最大键 长度是1000字节
运行spring boot应用程序后,我尝试在MySQL工作台中手动添加唯一约束。 -不起作用
我添加了以下代码-不起作用
@Table(name = "ingredient", uniqueConstraints=@UniqueConstraint(name="uk_ingredient_name",columnNames="ingredient_name"))
@Column(name = "ingredient_name" ,unique = true)
private String ingredientName;
@Entity
@Table(name = "ingredient")
public class Ingredient {
@Id
@Column(name="ingredient_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "ingredient_name" ,unique = true)
private String ingredientName;
我想保留所有重复的成分。我不想重复输入。 我经历了其他答案,但这些解决方案都没有帮助我。
答案 0 :(得分:0)
您可以删除
,unique = true
从@Column注释中尝试使用唯一的true时似乎有一个休眠错误,它不遵守命名策略。
有关更多信息,请看这篇文章
@UniqueConstraint and @Column(unique = true) in hibernate annotation
还有这个
https://hibernate.atlassian.net/browse/HHH-11586
在删除unique = true之后,我可以生成具有指定名称'uk_ingredient_name'的唯一约束。由于@Table批注中已经指定了唯一约束。
您得到的错误是因为hibernate生成的唯一约束名称达到了允许的限制,并且没有考虑到您声明的约束名称。
答案 1 :(得分:0)
我尝试在@column中添加length = 20。 它工作正常,没有任何错误。