简体   繁体   中英

How to extend a class in Spring Boot without changing the MySQL schema?

For example, I have the User class, with looks like the next:

    @Entity
    @Table(name = "users")
    public class User{
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;

        @NotBlank
        @Size(max = 40)
        @Column(name = "name")
        private String name;

        @NotBlank
        @Size(max = 15)
        @Column(name = "username")
        private String username;

        @NaturalId
        @NotBlank
        @Size(max = 40)
        @Email
        @Column(name = "email")
        private String email;

        @NotBlank
        @Size(max = 100)
        @Column(name = "password")
        private String password;

        @ManyToMany(fetch = FetchType.LAZY)
        @JoinTable(name = "user_roles",
                joinColumns = @JoinColumn(name = "user_id"),
                inverseJoinColumns = @JoinColumn(name = "role_id"))
        private Set<Role> roles = new HashSet<>();

//Constructor
//Getters and Setters

And I have the Client class:

@Entity
@Table(name = "cliente")
public class Cliente {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    @Column(name = "empresa")
    private String empresa;
    @Column(name = "telefono")
    private Integer telefono;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(unique = true)
    private Licencia licencia;

    @OneToMany(cascade = {
            CascadeType.PERSIST,
            CascadeType.REMOVE
    } ,fetch = FetchType.EAGER)
    @JoinTable(name = "user_cliente",
            joinColumns = @JoinColumn(name = "cliente_id"),
            inverseJoinColumns = @JoinColumn(name = "user_id"))
    private Set<User> users = new HashSet<>();

    public Cliente(String empresa, Integer telefono) {
        this.empresa = empresa;
        this.telefono = telefono;
    }

//Constructor
//Getters and Setters

Now, what I want to do is the Client class to extends the User class, so I can add a Client with name, username, email, etc. But I want two separate tables in MySQL, one for the users and its attributes, and other for clients only with the information of client, like the telephone or company. The problem is when I extends the User class in Client class, the MySQL databases updates and create the fields of telephone, company, etc. in the User table. How can I avoid this?

Use @MappedSuperclass :

@MappedSuperclass
public class BaseEntity {
    // here you can add common fields for your entities
}

and then extend from it:

@Entity
public class User extends BaseEntity {
    // user specific fields goes here
}

and Client :

@Entity
@Table(name = "cliente")
public class Cliente extends BaseEntity {
    // client specific fields here
}

For more info read How to inherit properties from a base class entity using @MappedSuperclass with JPA and Hibernate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM