Estou com algumas dúvidas em relação a herança. Estou criando um projeto, em que tenho a classe Usuario como classe pai, e Cliente e Administrador como classe filhos. Na classe usuário, tenho username, password e roles, mas quando tento atribuir a role a um cliente ou administrador recebo um erro. A classe Usuario está mapeada com @MappedSuperclass, então não é gerada no banco. código no github: https://github.com/RuanPablo1/E-commerce
Usuario:
@Data
@AllArgsConstructor
@NoArgsConstructor
@MappedSuperclass
//@Entity
//@Inheritance(strategy = InheritanceType.JOINED)
//@Table(name = "tb_usuarios")
public class Usuario implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_usuario")
private Long idUsuario;
@Column(name = "email")
private String email;
@Column(name = "password")
private String password;
@Column(name = "nome")
private String nome;
@ManyToMany
@JoinTable(name = "tb_usuarios_roles",
joinColumns = @JoinColumn(name = "id_usuario"),
inverseJoinColumns = @JoinColumn(name = "id_role"))
private List<Roles> roles;
}
Administrador:
@NoArgsConstructor
@Entity
@Table(name = "tb_administradores")
public class Administrador extends Usuario{
private static final long serialVersionUID = 1L;
public Administrador(Long idUsuario, String email, String password, String nome, List<Roles> roles) {
super(idUsuario, email, password, nome, roles);
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
return true;
}
}
Cliente:
@NoArgsConstructor
@Getter
@Setter
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "tb_clientes")
public class Cliente extends Usuario {
private static final long serialVersionUID = 1L;
@Column(name = "telefone")
private String telefone;
@OneToMany(mappedBy = "idPedido")
private List<Pedido> pedidos;
public Cliente(Long idUsuario, String email, String password, String nome, List<Roles> roles, Long idCliente,
String telefone, List<Pedido> pedidos) {
super(idUsuario, email, password, nome, roles);
this.telefone = telefone;
this.pedidos = pedidos;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + Objects.hash(pedidos, telefone);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Cliente other = (Cliente) obj;
return Objects.equals(pedidos, other.pedidos) && Objects.equals(telefone, other.telefone);
}
}
esquema no banco:
erro:
0 2 01:04:21 INSERT INTO tb_usuarios_roles(ID_USUARIO, ID_ROLE) VALUES(1, 1) Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (
db_ecommerce
.tb_usuarios_roles
, CONSTRAINT FK3ya6jkf8n7qfld457do6sdsq
FOREIGN KEY (id_usuario
) REFERENCES tb_clientes
(id_usuario
)) 0.031 sec