【问题标题】:hibernate doesn't create all the associated tableshibernate 不会创建所有关联的表
【发布时间】:2017-08-21 21:49:11
【问题描述】:

我有三个实体类。并且通过使用spring boot,我正在尝试创建所有这三个表。但它只创建了三个表中的一个。

我的所有三个班级都是:-

Users.java

@Entity
@Table(name = "users")
public class Users {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", nullable = false, updatable = false)
private long id;
private String username;
private String password;
private String firstName;
private String lastName;

@Column(name="email", nullable = false, updatable = false)
private String email;
private String phone;
private boolean enabled=true;

@OneToMany(mappedBy = "users", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JsonIgnore
private Set<UserRole> userRoles = new HashSet<>();

---------------------getter setters--------------------------------
}

UserRole.java

@Entity
@Table(name="user_role")
public class UserRole {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long userRoleId;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="user_id")
private Users users;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="role_id")
private Role role;

===============getter setters==============================
}

Role.java

@Entity
@Table(name="user_role")
public class UserRole {

@Id
private long roleId;
private String name;

@OneToMany(mappedBy = "role", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private Set<UserRole> userRoles = new HashSet<>();

==============getter setters ====================

我希望 hibernate 创建三个表,但在我的情况下,只创建了一个表角色,我收到类似的错误

原因:org.hibernate.tool.schema.spi.SchemaManagementException: 无法对 JDBC 目标执行模式管理 [create table user_role (user_role_id numeric(19,0) identity not null, role_id numeric(19,0), user_id numeric(19,0),主键(user_role_id))]

原因:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你的SQL语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 1 行的“identity not null, role_id numeric(19,0), user_id numeric(19,0), primary key (us') 附近使用

【问题讨论】:

    标签: java hibernate spring-boot


    【解决方案1】:

    错误表明您试图在 MySQL 数据库引擎中使用“IDENTITY NOT NULL”,这是无效的。 IDENTITY 是 SQL SERVER 关键字。

    确保您已为您的数据库引擎正确配置了 Hibernate。您应该使用 MySQLDialect。

    在您的 application.properties 中:

    spring.jpa.database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    
    spring.jpa.properties.hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    

    【讨论】:

    • 我把所有的@GeneratedValue 改成 Auto 还是一样的错误
    • 非常感谢。我实际上在 application.properties 中缺少 MySQL5InnoDBDialect。
    • @sagarlimbu 太棒了!如果您发现对问题有用的答案,最好将其标记为已批准。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 2021-05-28
    • 2017-08-23
    相关资源
    最近更新 更多