5

I want to create new table on my database using this class

@Entity
@Table(name = "currency_rate")
public class CurrencyRate {

    @Id
    private String id;

    @Column(name = "source_currency")
    private String sourceCurrency;

    @Column(name = "target_currency")
    private String targetCurrency;

    @Column(name = "exchange_rate")
    private double exchangeRate;

    @Column
    private Date date;

    @PrePersist
    public void generateID() {            
        this.id = this.date.toString().replace("-", "") + sourceCurrency + targetCurrency;
    }
    //getters, setters
}

When I try to run my application with property

spring.jpa.hibernate.ddl-auto=create

I got this exception

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Specified key was too long; max key length is 1000 bytes

Looks like I can't use Spring as my ID? Changing type to Long solves problem, but I really wanted to go with String with this one. From what I searched, it should be totally doable.

Weeedooo
  • 501
  • 8
  • 25
  • Possible duplicate of [How to use @Id with String Type in JPA / Hibernate?](https://stackoverflow.com/questions/18622716/how-to-use-id-with-string-type-in-jpa-hibernate) – Yassin Hajaj Jun 02 '18 at 20:38
  • @YassinHajaj: No, it is not a duplicate. Please dont't generate SPAM. If you cannot exactly tell if this is a duplicate, then press SKIP when you are reviewing questions. LOW QUALITY comments are as bad as low quality questions. – mentallurg Jun 02 '18 at 20:47
  • @mentallurg This is why the sentence starts with *Possible duplicate of* – Yassin Hajaj Jun 02 '18 at 21:07
  • i found a solution https://stackoverflow.com/questions/1459265/hibernate-create-mysql-innodb-tables-instead-of-myisam – Dyakin Anton Apr 04 '20 at 14:12

1 Answers1

0

As per tutorial from https://www.tutorialspoint.com/hibernate/hibernate_annotations.htm, the attribute can be defined by Column annotation in details.

@Column Annotation The @Column annotation is used to specify the details of the column to which a field or property will be mapped. You can use column annotation with the following most commonly used attributes −

name attribute permits the name of the column to be explicitly specified.

length attribute permits the size of the column used to map a value particularly for a String value.

nullable attribute permits the column to be marked NOT NULL when the schema is generated.

unique attribute permits the column to be marked as containing only unique values.

What matters here for your question is length parameter, maybe you can try annotate your id like below:

  @Id
  @Column(length = 100)
  private String id;
Eugene
  • 10,627
  • 5
  • 49
  • 67