0

I have created an entity with table name and column names. I have also added the uniquekey constraint for a column name. But when I run, it shows the following error ;

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table ingredient add constraint UK_co7ro6kyijhfik027h0y4d3n3 unique (ingredient_name).

java.sql.SQLSyntaxErrorException: Specified key was too long; max key length is 1000 bytes

  1. After I run the spring boot application, I have tried to add the unique constraint manually in MySQL workbench. - DOES NOT WORK

  2. I have added the below code - DOES NOT WORK

    @Table(name = "ingredient", uniqueConstraints=@UniqueConstraint(name="uk_ingredient_name",columnNames="ingredient_name"))

    @Column(name = "ingredient_name" ,unique = true)
    private String ingredientName;
  1. Tried to create a table manually in Mysql workbench and tried to alter the column name with unique key later. THIS WORKS. But I want hibernate to do this for me.
    @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; 

I want to save the ingredients without any repetitions. I do not want duplicate entries. I have gone through other answers and none of those solutions helped me.

P H
  • 294
  • 1
  • 3
  • 16
  • Possible duplicate of [#1071 - Specified key was too long; max key length is 1000 bytes](https://stackoverflow.com/questions/8746207/1071-specified-key-was-too-long-max-key-length-is-1000-bytes) – Himanshu Bhardwaj Jun 10 '19 at 12:22
  • Please do a search before - https://stackoverflow.com/questions/8746207/1071-specified-key-was-too-long-max-key-length-is-1000-bytes . Just limit the length of ingredientName field. – Himanshu Bhardwaj Jun 10 '19 at 12:23
  • Possible duplicate of [Error: #1071 - Specified key was too long; max key length is 1000 bytes - mysql 5.0.91](https://stackoverflow.com/questions/10642429/error-1071-specified-key-was-too-long-max-key-length-is-1000-bytes-mysql) – Xavier Bouclet Jun 10 '19 at 13:14
  • I have gone through other answers and none of those solutions helped me. They have given solutions for MySQL which I didn't require. I finally figured it was because of the length attribute in @Column. This solution is not present in any of the questions which you have sent. – P H Jun 10 '19 at 15:02

3 Answers3

1

Could you remove

,unique = true

from @Column annotation and try, it seems there is an hibernate bug .When using unique true it does not obey the naming strategy.

For more information look at this post

@UniqueConstraint and @Column(unique = true) in hibernate annotation

and this one

https://hibernate.atlassian.net/browse/HHH-11586

After removing the unique=true I am able to generate the unique constraint with the specified name that is 'uk_ingredient_name'.Since it is already specified in the @Table annotation.

The error you are getting is because hibernate is generating a unique constraint name which is hitting the allowed limit and it is not taking into the account the constraint name you have declared.

user06062019
  • 681
  • 4
  • 9
  • Thank you, but this did not help. I had already tried this way , that is, removing unique = true . I got the same error. – P H Jun 10 '19 at 14:52
1

I tried adding length = 20 in the @column. It works completely fine without any error.

P H
  • 294
  • 1
  • 3
  • 16
0
@Column(name="username", length=8, unique=true)

worked fine for me.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36