Wee*_*ooo 5 mysql spring spring-data
我想使用这个类在我的数据库上创建新表
@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
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用属性运行我的应用程序时
spring.jpa.hibernate.ddl-auto=create
Run Code Online (Sandbox Code Playgroud)
我得到了这个例外
引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:指定的键太长;最大密钥长度为 1000 字节
好像不能用 Spring 作为我的 ID?将类型更改为 Long 可以解决问题,但我真的很想将 String 与这个一起使用。从我搜索的内容来看,它应该是完全可行的。
根据https://www.tutorialspoint.com/hibernate/hibernate_annotations.htmColumn的教程,该属性可以通过注释详细定义。
\n\n\n@Column 注释 @Column 注释用于指定字段或属性将映射到的列的\n 详细信息。您\n 可以将列注释与以下最常用\n 属性一起使用\xe2\x88\x92
\n\nname 属性允许显式指定列的名称。
\n\nlength 属性允许用于映射值的列的大小,特别是对于字符串值。
\n\nnullable 属性允许在生成架构时将列标记为 NOT NULL。\n
\n\nunique 属性允许将列标记为仅包含\n 唯一值。
\n
对于您的问题来说,重要的是length参数,也许您可以尝试像下面这样注释您的 id:
@Id\n @Column(length = 100)\n private String id;\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
904 次 |
| 最近记录: |