Spring Boot Hibernate 创建名称错误的表
Spring Boot Hibernate creates tables with wrong names
当我 运行 我的项目时,Hibernate 会自动创建 table 名称错误的项目。
我有两个 tables 用户和角色,还有三个 classes:
摘要 class IdField.java:
@Entity
public abstract class IdField {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
//constructors and getters setters
User.java class:
@Entity
@Table(name = "user", schema = "quiz_app")
public class User extends IdField{
@Column(name = "user_name")
private String userName;
@Column(name = "password")
private String password;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
@ManyToMany(fetch = FetchType.EAGER)
private Collection<Role> roles = new ArrayList<>();
//constructors and getters setters
和Role.java class:
@Entity
@Table(name = "role", schema = "quiz_app")
public class Role extends IdField{
@Enumerated(EnumType.ORDINAL)
@Column(name = "role_name")
private RoleName roleName;
//constructors and getters setters
并且 Hibernate 创建了两个 tables,名称错误,如 id_field 和 id_field_roles:
但我想要 table 名称,因为它在 @Table 注释中,如“用户”和“角色”
熟悉继承策略:
https://thorben-janssen.com/complete-guide-inheritance-strategies-jpa-hibernate/
在我看来你正在寻找@MappedSuperclass
If you just want to share state and mapping information between your entities, the mapped superclass strategy is a good fit and easy to implement. You just have to set up your inheritance structure, annotate the mapping information for all attributes and add the @MappedSuperclass annotation to your superclass. Without the @MappedSuperclass annotation, Hibernate will ignore the mapping information of your superclass.
最重要的是:如果您的共享部分只是 id 字段,顾名思义,继承看起来有点矫枉过正。
当我 运行 我的项目时,Hibernate 会自动创建 table 名称错误的项目。 我有两个 tables 用户和角色,还有三个 classes: 摘要 class IdField.java:
@Entity
public abstract class IdField {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
//constructors and getters setters
User.java class:
@Entity
@Table(name = "user", schema = "quiz_app")
public class User extends IdField{
@Column(name = "user_name")
private String userName;
@Column(name = "password")
private String password;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
@ManyToMany(fetch = FetchType.EAGER)
private Collection<Role> roles = new ArrayList<>();
//constructors and getters setters
和Role.java class:
@Entity
@Table(name = "role", schema = "quiz_app")
public class Role extends IdField{
@Enumerated(EnumType.ORDINAL)
@Column(name = "role_name")
private RoleName roleName;
//constructors and getters setters
并且 Hibernate 创建了两个 tables,名称错误,如 id_field 和 id_field_roles:
但我想要 table 名称,因为它在 @Table 注释中,如“用户”和“角色”
熟悉继承策略:
https://thorben-janssen.com/complete-guide-inheritance-strategies-jpa-hibernate/
在我看来你正在寻找@MappedSuperclass
If you just want to share state and mapping information between your entities, the mapped superclass strategy is a good fit and easy to implement. You just have to set up your inheritance structure, annotate the mapping information for all attributes and add the @MappedSuperclass annotation to your superclass. Without the @MappedSuperclass annotation, Hibernate will ignore the mapping information of your superclass.
最重要的是:如果您的共享部分只是 id 字段,顾名思义,继承看起来有点矫枉过正。