我是一名新开发人员。我正在尝试发布“leilao”信息,但我的数据库(leilao)中有一个列,其中外键(iduser)将id引用到我的其他列(usuario)id。我在我的类 Leilao.java 中使用注释 @ManyToOne 和 @JounColumn。这里有什么问题吗? 感谢您的帮助!
我希望使用 id 发布与 usuario 列相关的“leilao”信息。
package br.com.leilao.leiloesapi.leilao;
import jakarta.persistence.\*;
import java.time.LocalDate;
import br.com.leilao.leiloesapi.usuario.Usuario;
import lombok.\*;
@Table(name = "leiloes")
@Entity(name = "Leiloes")
@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = "id")
public class Leilao {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "iduser")
private Usuario usuario;
private String name;
private Double price;
private LocalDate data;
public Leilao(DadosCadastroLeilao dadosCadastroLeilao) {
this.name = dadosCadastroLeilao.name();
this.price = dadosCadastroLeilao.price();
this.data = dadosCadastroLeilao.data();
}
}
create table leiloes(
id bigint not null auto_increment,
iduser bigint not null,
name varchar(100) not null,
price decimal not null,
data date not null,
primary key(id),
constraint fk_leiloes_iduser foreign key(iduser) references usuarios(id)
);
package br.com.leilao.leiloesapi.usuario;
import jakarta.persistence.\*;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Table(name = "usuarios")
@Entity(name = "Usuario")
@Getter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = "id")
public class Usuario {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String username;
private String password;
public Usuario(DadosCadastroUsuario dadosCadastroUsuario) {
this.name = dadosCadastroUsuario.name();
this.username = dadosCadastroUsuario.username();
this.password = dadosCadastroUsuario.password();
}
}
{
"iduser": 25,
"name": "Iphone 14",
"price": "14000",
"data": "2023-11-20"
}
我使用的是JAVA 17,Spring Boot和MYSQL数据库。