我想把两张table联系起来
用户和地址用户只有一个地址,一个地址只属于一个用户。密钥是按地址的id列出的,因此我先创建地址,然后创建一个用户并将其与地址id链接,但我根本无法这样做,因此我返回以下错误:
Error creating bean with name 'entityManagerFactory' defined in class path resource [org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NullPointerException: Cannot invoke "org.hibernate.mapping.PersistentClass.getTable ()" because "classMapping" is null
我是一个全新的冬眠,但我需要这个项目的大学,所以请原谅我对这个问题的无知
这就是我的代码:
用户/用户类别:
import org.hibernate.validator.constraints.br.CPF;
import javax.persistence.*;
import javax.validation.constraints.*;
public class Usuario{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column
@NotNull
@Size(min = 5,max = 30)
@Pattern(regexp = "^[a-zA-Z\s]*$", message = "Nome inválido! Digite apenas letras e espaçamento") //Permite apenas letras e espaço
private String nome;
@NotNull
@CPF
private String cpf;
@NotNull
@Email
private String email;
@NotNull
@Size(min = 5,max = 12)
private String senha;
private Integer telefone;
@DecimalMin("0")
@DecimalMax("5")
private Double avaliacao;
@NotNull
@OneToOne(cascade = CascadeType.ALL,mappedBy = "id")
private Endereco endereco;
//Atributos para usuários autônomos
private Boolean isAutonomo;
private String categoriaAutonomo;
private Double precoAutonomo;
//Getters and Setters
地址/endereco类
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Entity
@Table(name = "endereco")
public class Endereco {
@Id
@OneToOne
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@NotNull
@Size(min = 8,max = 8)
private String cep;
@NotNull
private String bairro;
@NotNull
private String logradouro;
@NotNull
private Integer numeroLogradouro;
private String complemento;
@NotNull
@Size(min = 2,max = 2)
private String uf;
@NotNull
private String cidade;
控制器
import br.com.bandtec.projetocaputeam.dominio.*;
import br.com.bandtec.projetocaputeam.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/caputeam")
public class CaputeamController {
@Autowired
private UsuariosRepository usuariosRepository;
@Autowired
private EnderecoRepository enderecoRepository;
//--- USERS
@GetMapping("/usuarios")
public ResponseEntity getUsuarios(){
List<Usuario> usuarios = usuariosRepository.findAll();
return !usuarios.isEmpty() ? ResponseEntity.status(200).body(usuarios) :
ResponseEntity.status(204).build();
}
@PostMapping("/cadastrar-usuario")
public ResponseEntity cadastrarUsuario(@RequestBody @Valid Usuario novoUsuario){
usuariosRepository.save(novoUsuario);
return ResponseEntity.ok().build();
}
//--- ADRESS
@PostMapping("/cadastrar-endereco")
public ResponseEntity cadastrarEndereco(@RequestBody @Valid Endereco novoEndereco){
enderecoRepository.save(novoEndereco);
return ResponseEntity.ok().build();
}
}
应用
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProjetoCaputeamApplication {
public static void main(String[] args) {
SpringApplication.run(ProjetoCaputeamApplication.class, args);
}
}
这就是我的逻辑模型
编辑我试图删除“mapped by”部分并从address中删除@onetoone,但现在当我尝试发送post of address时,它返回以下错误:
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Referential integrity constraint violation: "FKMXNOON0IKGA83W1A203Y6OFPN: PUBLIC.ENDERECO FOREIGN KEY(ID) REFERENCES PUBLIC.USUARIO(ID) (1)"; SQL statement:
insert into endereco (bairro, cep, cidade, complemento, logradouro, numero_logradouro, uf, id) values (?, ?, ?, ?, ?, ?, ?, ?) [23506-200]
好像他没有输入任何地址字段
我是这样用 Postman 寄信的:
{
"bairro": "Vila Prmavera",
"cep": "03388110",
"cidade": "São Paulo",
"complemento": "b1",
"logradouro": "Rua das dores",
"numeroLogradouro": 7,
"uf": "SP"
}
1条答案
按热度按时间vhipe2zx1#
不在id上Map。Map表示实体Map而不是idMap。
或者如果你不想endereco保存对usuario的引用,就把它删除。但你不能
@OneToOne
在id字段上。如果你只有一面@OneToOne
然后还需要注解@MapsId
.因为
@OneToOne
尝试与表示数据库中的表的实体进行Map。对于id,数据库中没有任何实体或表。这就是它抱怨的原因