【问题标题】:Does Hibernate has a specific condition for naming columns that start with the same letter?Hibernate 是否有特定条件来命名以相同字母开头的列?
【发布时间】:2019-03-31 15:37:28
【问题描述】:

我正在使用 spring boot 和 jpa hibernate 在我的数据库中创建一些表,但它引发了一个我不太了解的奇怪异常。

@Entity
public class Option {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String marca;
    private String model;
    private String anFabrDeLa;
    private String anFabrPanaLa;
    private String pretDeLa;
    private String pretPanaLa;
    private String oras;
    private int score;
    //constructors, getters and setters

}

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'option (id integer not null, an_fabr_de_la varchar(255), an_fabr_pana_la varchar' at line 1

我尝试使用@Column(name='something'),但它仍然给我这个奇怪的错误...

【问题讨论】:

    标签: hibernate spring-boot jpa


    【解决方案1】:

    option 和 options 是 MySQL 使用的关键字。请使用一些不同的名称。 https://dev.mysql.com/doc/refman/8.0/en/keywords.html

    【讨论】:

      【解决方案2】:

      option 是一个MySQL keyword。选择另一个表名。

      【讨论】:

        【解决方案3】:

        正如人们已经说过的,您使用 MySql 保留关键字作为表名。 如果你想保留你的类名但想重命名你的表,你必须添加

        @Table(name="something") // This will explicitly set the table name.
        @Entity
        public class Option {
           ...
        }
        

        或者如果你真的想保留类名和表名,你必须把表名放在`标记之间:

        @Table(name="`option`")
        @Entity
        public class Option {
           ...
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-11-10
          • 1970-01-01
          • 2011-08-06
          • 2019-10-22
          • 2018-03-15
          • 2016-04-14
          • 2014-11-26
          • 2023-02-20
          相关资源
          最近更新 更多