Olá, eu estou tendo uma exceção estranha que eu não entendi porque deu, já que me código deve estar igual ao do instrutor. Eis aqui a exceção:
Hibernate:
create table Clientes (
id bigint not null auto_increment,
cpf varchar(255),
nome varchar(255),
primary key (id)
) engine=InnoDB
Hibernate:
create table Itens_Pedido (
id bigint not null auto_increment,
precoUnitario decimal(19,2),
quantidade integer not null,
pedido_id bigint,
produto_id bigint,
primary key (id)
) engine=InnoDB
Hibernate:
create table Pedidos (
id bigint not null auto_increment,
data date,
valorTotal decimal(19,2),
cliente_id bigint,
primary key (id)
) engine=InnoDB
Hibernate:
alter table Itens_Pedido
add constraint FK9qm4b0v7p0bxyax485xpw5t0u
foreign key (pedido_id)
references Pedidos (id)
Hibernate:
alter table Itens_Pedido
add constraint FK473htjwdtniq0tuo8pmo2bo37
foreign key (produto_id)
references Produtos (id)
mai. 13, 2021 10:09:39 AM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
WARN: GenerationTarget encountered exception accepting command : Error executing DDL "
alter table Itens_Pedido
add constraint FK473htjwdtniq0tuo8pmo2bo37
foreign key (produto_id)
references Produtos (id)" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
alter table Itens_Pedido
add constraint FK473htjwdtniq0tuo8pmo2bo37
foreign key (produto_id)
references Produtos (id)" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applyForeignKeys(AbstractSchemaMigrator.java:433)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:249)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:316)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:469)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1259)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at br.com.alura.loja.util.JPAUtil.<clinit>(JPAUtil.java:10)
at br.com.alura.loja.main.Main.procurarProduto(Main.java:23)
at br.com.alura.loja.main.Main.main(Main.java:18)
Caused by: java.sql.SQLException: Referencing column 'produto_id' and referenced column 'id' in foreign key constraint 'FK473htjwdtniq0tuo8pmo2bo37' are incompatible.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:762)
at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:646)
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
... 16 more
Classe pedidos (omitirei os getters e setters):
@Entity
@Table(name = "Pedidos")
public class Pedido {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private BigDecimal valorTotal;
private LocalDate data = LocalDate.now();
@ManyToOne
private Cliente cliente;
@OneToMany(mappedBy = "pedido")
private List<ItemPedido> itens;
public Pedido(){}
public Pedido(Cliente cliente) {
this.cliente = cliente;
}