Я хочу создать новую таблицу в своей базе данных, используя этот класс
@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
}
Когда я пытаюсь запустить приложение со свойством
spring.jpa.hibernate.ddl-auto=create
У меня есть это исключение
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Specified key was too long; max key length is 1000 bytes
Похоже, я не могу использовать Spring в качестве идентификатора? Изменение типа на Long решает проблему, но я действительно хотел использовать String с этим. Судя по тому, что я искал, это должно быть вполне выполнимо.
@YassinHajaj: Нет, это не дубликат. Пожалуйста, не создавайте СПАМ. Если вы не можете точно определить, является ли это дубликатом, нажмите «ПРОПУСТИТЬ» при просмотре вопросов. Комментарии НИЗКОГО КАЧЕСТВА так же плохи, как и вопросы низкого качества.
@mentallurg Вот почему предложение начинается с Возможный дубликат
я нашел решение stackoverflow.com/questions/1459265/…






Согласно руководству из https://www.tutorialspoint.com/hibernate/hibernate_annotations.htm, атрибут можно детально определить с помощью аннотации Column.
@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.
Для вашего вопроса здесь важен параметр length, возможно, вы можете попробовать аннотировать свой идентификатор, как показано ниже:
@Id
@Column(length = 100)
private String id;
Возможный дубликат Как использовать @Id со строковым типом в JPA / Hibernate?